Two’s complement in Binary

Two’s complement is a mathematical operation in binary numbers that allows for the signed numbers. In order to represent negative numbers in binary, the most significant bit is called the sign bit. Note that the examples below uses big endian.

Initially, I wondered as to why you couldn’t simply reserve the left-most bit for signed or unsigned representation: 1 reserved for negative and 0 for positive. When I did that, I got weird numbers for addition (see below).

The way to solve this problem is by using two’s complements

Steps to represent negative numbers in bits

  1. Complement the bits
  2. Add one to the result

Step 1) Complement the bits with one’s complement

One’s complement still reserves the left-most bit as signed and unsigned representation. However, the negative numbers are the inverse of its positive counterpart. So what happens when you do addition? It’s closer to what you would have wanted; however, you are one off from the expected value (see below). To address this issue, you simply add 1 to the one’s complement.

Step 2) Add one to the one’s complement

Adding one resolves the problem, and we get the expected output when we do bit math.

In the examples above, the left-most bit has mathematical significance. Let used -7 as an example, 1001. The 1 is in the 2³ place, so we have -8. There is also a 1 in the 2⁰ position, so we get 1. -8 + 1 = -7.