Negative numbers representation

Manuel Puerta Villa
4 min readMar 27, 2020

--

I’m Manuel and today I will reinforce my knowledge with you. Following the Feynman technique I will try to teach you what is two’s complement and how integers are stored in memory using it. If at the end you learned something please let me know!

Negative numbers are amazing. They are an incredible display of the abstraction and human creativity, seriously. Did you ever had minus 5 apples? I'm sure you don't. Negatives numbers became so incredible normal in our lives that its really hard to even understand a time with out them. Now our mission is to get our abstraction into the computers.

We will have a journey trough some ways in how we can represent negative number in computers.

Sign Bit

The first idea that comes to my mind is to have a space in the computer (1 bit) where we can have a standard. If this is set as ‘1’ is negative and if ‘0’ is positive. Ideally this will be a very clear place and will not mix up with the number itself. So what about the left bit? Sounds good, lets do it.

  • 00000101 in binary→ 5
  • 10000101 in binary → -5

This is a first approximation for binary numbers in computers. Looks good but it can have many problems, some of them are the difficulty of good implementation in software and hardware, the possibility of overflow destroying the sign bit and one of the funniest ones:

  • 00000000 in binary→ +0
  • 10000000 in binary → -0

Wait … What?! ‘+0’ and ‘-0’ ? Well, yes. Not the ideal system for our negative numbers.

One’s complement

One’s complement is more advance that our first approach. Really smart and yet very simple. Everything you have to do is change all bits, if they are zero change it to one, if one change it to zero.

  • 00000101 in binary→ 5
  • 11111010 in binary→ -5

Kind of weird, I know. It’s not the most intuitive approach but is very powerful. The sum operation it’s now very easy to do. Here is one example:

Can you figure out how it works? The trick is to sum the overflow bit again!

Funny fact:

  • 00000000 in binary→ +0
  • 11111111 in binary → -0

Yes… This system still have the double zero problem :(

Two’s complement

Two’s complement is our last representation system and is very similar to one’s complement. You flip all the bits as in one’s complement and then add ‘1’ to the result. Yes, that’s all.

  • 00000101 in binary→ 5
  • 11111010 in binary→ -5 in One’s complement
  • 11111011 in binary→ -5 in Two’s complement

Now the zero:

  • 00000000 in binary →0
  • 00000000 in binary →0 in Two’s complement

The double zero problem is solve!!!

-95 in Two’s complement

Now lets make a sum!

sum of 15 and -5 in Two’s complement

In Two’s complement there is no need to sum again the overflow bit as in One’s complement. Here the overflow bit get completely ignore, no love for him.

Lets see more examples:

15 minus -5 in Two’s complement

Works perfect in subtraction operations.

Two’s complement is the most used and implement representation system for negative numbers in computers. The reason is because is the easiest to implement in hardware.

In resume Two’s complements is very good doing the fundamental arithmetic operations of addition, subtraction, and multiplication because you don’t need to think in the negative number, the procedure is the same as unsigned binary numbers, solves the problem of the double zero and is the easiest to implement in hardware.

That’s all I know. Hopefully you learned something!

Have a good day!

Holberton School, Medellin 2020

--

--