In the previous article we have seen how to represent positive integers, which was quite easy. Now we are going to see how to represent negative integers, which is a little more “tricky”.

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

But, instead of simply explaining what the 2’s complement is, we will try to reason why we use this system, and how this representation was reached.

## Representing negative numbers in binary

Suppose that **we are “inventing” how to represent negative numbers in binary**. Let’s look at the logical steps we would end up coming to the 2’s complement.

### Attempt A: One bit for the sign

The first thing that occurs to us is, “we are going to use the leftmost bit to indicate that a number is negative.

The rest we leave the same. The other bits will be the same for the positive and 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 have done something that is quite well understood by humans, but it complicates the calculations a lot. So let’s take a turn.

### Attempt B: One’s complement

The bit for the sign went well. Also, we don’t have another choice. For the rest, let’s try one thing, **let’s flip 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 have improved… flipping the numbers has been a good “trick”, because **bit by bit the positive and the negative cancel each other out**, resulting in all 1’s, which we said is a 0.

But man **Now we have 2 different 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 take another look and see if we can fix it.

### Bingo!: 2’s complement

We have already reached the definitive one. We do the same

- Bit for the sign
- Negative numbers negated
- Also,
**we add ‘1’ to the negatives**

This is how the numbers from -3 to 3 would look like.

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 something strange. It gives zero, just one zero.

Just like before, by flipping the negative numbers we cause that when adding a negative and a positive they “couple” giving a sequence of all 1’s.

But, besides, as we are now adding an extra 1 (which carries the negative number), **all those 1’s overflow and become zero**. And that’s why we use the 2’s complement.

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

## Your friend the 2’s complement

Summary of everything we have seen, the 2’s complement is a way of **representing negative numbers in the binary system**.

At first glance it seems complicated, but it is used a lot because **it allows us to operate with negative numbers in a simple way**. It makes counting much easier.

If we want to convert a positive number into a negative one, in binary system representation in 2’s complement:

- We place a
`1`

in the leftmost bit, which represents the sign - We flip the rest of the bits
- We add 1 to the previous one

## How much is a binary number worth?

I return to this, because it is something that people get confused about a lot. How much 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 does not represent anything, if you do not tell me what it represents.

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 tell me what you are representing, an integer, an integer with negatives, if it’s a float.