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 is a bit “more involved”.
In binary, we typically represent binary numbers in two’s complement. It’s a technique that simplifies calculations when working with negative numbers (although it’s a bit more complicated for humans to understand).
But, instead of just explaining what two’s complement is, let’s try to reason why we use this system and how it was arrived at.
Here comes a “heavy” explanation of how the two’s complement system came about
If you’re not interested and just want to see how it works, skip to the next section
Representing Negative Numbers in Binary
Let’s suppose we are “inventing” how to represent negative numbers in binary. Let’s see the logical steps that would eventually lead us to two’s complement.
Attempt A: A Bit for the Sign
The first thing that occurs to us 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 the positive and the negative. So, in 4 bits, -3 to 3 would look like this.
| Sign | Bit1 | Bit2 | Bit3 | Decimal |
|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 3 |
| 0 | 0 | 1 | 0 | 2 |
| 0 | 0 | 0 | 1 | 1 |
| 0 | 0 | 0 | 0 | 0 |
| 1 | 0 | 0 | 0 | -1 |
| 1 | 0 | 0 | 1 | -2 |
| 1 | 0 | 1 | 0 | -3 |
Okay, very easy to understand. But if we add -3 and 3… we get -6.
| Sign | Bit1 | Bit2 | Bit3 | Decimal | |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 3 | |
| + | 1 | 0 | 1 | 0 | -3 |
| = | 1 | 1 | 0 | 1 | -6 |
With this first attempt, we’ve done something that is quite understandable for humans, but that complicates calculations a lot. So let’s think about it some more.
Attempt B: One’s Complement
The sign bit thing worked out well. Also, we don’t really have another choice. For the rest, let’s try something, let’s invert the bits of the negative numbers.
So, the numbers from -3 to 3 would look like this.
| Sign | Bit1 | Bit2 | Bit3 | Decimal |
|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 3 |
| 0 | 0 | 1 | 0 | 2 |
| 0 | 0 | 0 | 1 | 1 |
| 0 | 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 | -0 |
| 1 | 1 | 1 | 0 | -1 |
| 1 | 1 | 0 | 1 | -2 |
| 1 | 1 | 0 | 0 | -3 |
Now, if we add them, this is the result.
| Sign | Bit1 | Bit2 | Bit3 | Decimal | |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 3 | |
| + | 1 | 1 | 0 | 0 | -3 |
| = | 1 | 1 | 1 | 1 | -0 |
Okay, we’ve improved… inverting the numbers was a good “trick”, because bit by bit the positive and negative cancel each other out, resulting in all 1’s, which we said is a 0.
But dude, now we have 2 distinct zeros! We have +0 and -0. And also, the sum gives -0, not 0.
That’s a bit weird. In the long run, it’s sure to give us problems. So let’s think about it some more to see if we can fix it.
Bingo!: Two’s Complement
Here we are at the definitive one. We do the same
- Bit for the sign
- Negative numbers negated
- Additionally, we add ‘1’ to the negatives
So the numbers from -3 to 3 would look like this.
| Sign | Bit1 | Bit2 | Bit3 | Decimal |
|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 3 |
| 0 | 0 | 1 | 0 | 2 |
| 0 | 0 | 0 | 1 | 1 |
| 0 | 0 | 0 | 0 | 0 |
| 1 | 1 | 1 | 1 | -1 |
| 1 | 1 | 1 | 0 | -2 |
| 1 | 1 | 0 | 1 | -3 |
Now we add -3 and 3 and…
| Sign | Bit1 | Bit2 | Bit3 | Decimal | |
|---|---|---|---|---|---|
| 0 | 0 | 1 | 1 | 3 | |
| + | 1 | 1 | 0 | 1 | -3 |
| = | 0 | 0 | 0 | 0 | 0 |
Bingo! It gives 0. Not -0 or some weird thing. It gives zero, just one zero.
Just like before, inverting the negative numbers causes a negative and a positive to “couple” when added, giving a sequence of all 1’s.
But, additionally, because we now add an extra 1 (which the negative number carries), all those 1’s overflow and become 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. Good, that’s appreciated, but it also happened with the other examples.
Your Friend Two’s Complement
Summary of everything we’ve seen, two’s complement is a form of representation of negative numbers in the binary system.
At first glance it seems complicated, but it’s used a lot because it allows us to operate with negative numbers in a simple way (it makes calculations much easier).
If we want to convert a positive number to negative, in binary system representation using two’s complement:
- We set the leftmost bit to
1, which represents the sign - We invert the rest of the bits
- We add 1 to the above
What is a Binary Number Worth?
I come back to this, because it’s something that confuses a lot of people. What is a binary number worth? 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 doesn’t represent anything, 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’s a float.
