Language: EN

representar-numeros-negativos-en-binario

How to represent negative integers in binary

In the previous article, we saw how to represent positive integers (which was quite easy). Now we are going to see how to represent negative integers, which has a bit “more to it”.

In binary, we normally represent binary numbers in two’s complement. It is a technique that simplifies calculations when working with negative numbers (although it is somewhat more complicated for humans to understand).

But instead of simply explaining what two’s complement is, let’s try to reason why we use this system and how we arrived at this representation.

A “heavy” explanation of how the two’s complement system came about
If you are not interested and just want to see how it works, jump to the next section

Representing negative numbers in binary

Let’s assume we are “inventing” how to represent negative numbers in binary. We will see the logical steps that would lead us to arrive at two’s complement.

Attempt A: One bit for the sign

The first thing that comes to mind is, “let’s use the leftmost bit to indicate that a number is negative”.

We leave the rest the same. The other bits will be the same for positive and negative. So, in 4 bits, -3 to 3 would look like this.

SignBit1Bit2Bit3Decimal
00113
00102
00011
00000
1000-1
1001-2
1010-3

Okay, very easy to understand. But if we add -3 and 3… we get -6.

SignBit1Bit2Bit3Decimal
00113
+1010-3
=1101-6

With this first attempt, we have done something that is quite understandable for humans, but it complicates calculations a lot. So let’s rethink it.

Attempt B: One’s Complement

The bit for the sign has worked well. Also, we have no other choice. For the rest, let’s try something, let’s invert the bits of negative numbers.

Thus, the numbers from -3 to 3 would look like this.

SignBit1Bit2Bit3Decimal
00113
00102
00011
00000
1111-0
1110-1
1101-2
1100-3

Now, if we add them, this is the result.

SignBit1Bit2Bit3Decimal
00113
+1100-3
=1111-0

Okay, we have improved… inverting the numbers has been a good “trick” because bit by bit the positive and negative cancel each other out, resulting in all 1’s, which we have said is a 0.

But dude Now we have 2 distinct zeros! We have +0 and -0. Moreover, the sum results in -0, not 0.

That is a bit strange. In the long run, it will surely cause us problems. So let’s rethink it to see if we can fix it.

Bingo!: Two’s Complement

We have finally arrived at the definitive solution. We do the same

  • Bit for the sign
  • Negative numbers negated
  • Additionally, we add ‘1’ to the negatives

Thus, the numbers from -3 to 3 would look like this.

SignBit1Bit2Bit3Decimal
00113
00102
00011
00000
1111-1
1110-2
1101-3

Now we add -3 and 3 and…

SignBit1Bit2Bit3Decimal
00113
+1101-3
=00000

Bingo! It gives 0. Not -0 or some strange thing. It gives zero, just one zero.

Just like before, by inverting the negative numbers, we caused a situation where adding a negative and a positive “couples” to give a sequence of all 1’s.

But, in addition, since now we add an extra 1 (which carries the negative number), all those 1’s overflow and turn into zero. And that’s why we use two’s complement.

Finally, and if you were wondering, the two’s complement of the two’s complement of a number is the number itself. That is, -(-3) = 3. Well, that’s appreciated, but it also happened with the other examples.

Your friend two’s complement

In summary of everything we have seen, two’s complement is a way of representing negative numbers in binary system.

At first glance, it seems complicated, but it is widely used because it allows us to operate with negative numbers easily (it greatly simplifies calculations).

If we want to convert a positive number to negative in binary representation in two’s complement:

  • Set the left bit, which represents the sign, to 1
  • Invert the rest of the bits
  • Add 1 to the previous result

How much is a binary number worth?

I return to this, because it’s something that confuses a lot of people. How much is a binary number worth? Let’s imagine you have this number:

10011001

How much is this number worth?

  • Some of you will tell me 153
  • Others will tell me -103

How can that be? Because a binary number represents nothing unless you tell me what system it uses to represent it.

The binary representation is the same. But the number they represent is NOT the same.

To know what number it is, I need you to say what you are representing, an integer, an integer with negatives, if it is a float.