Two's Complement and Negative Binary Numbers

Numbers are commonly represented inside the computer using binary, and for GCSE you generally only need to consider positive integers - also known as natural numbers. One way of representing negative numbers in binary is to use two's complement, but how and why does that work?

Think of a memory location, or a register, as being like a counter - e.g. like the odometer on a older car. When you reach the maximum value that it can display, it overflows - the numbers roll around, e.g. from 99999 to 00000.

What would happen if you ran the counter backwards? Click the button to see:

Denary

If you counted down in denary, then a similar thing would happen in the opposite direction:

Shadow
Shadow
Shadow

Note that this only works because we've fixed the number of digits - if you were counting in your head there would be no maximum value and the number wouldn't wrap around in the same way.

Binary

So what would happen in binary? Remember that the key thing to understand about binary is that it's behavious is consistent with that of number bases. The same thing would happen - 00000 would change to 11111 and vice versa:

Shadow
Shadow
Shadow
Shadow
Shadow

Note that both counters here count down from fifteen, but, as with the denary counter, the exact appearance of the numbers will depend on the number of bits being used to represent them.

Negative Binary Numbers

Can we represent a negative number without counting down? The method shown about is called two's complement, and generating the two's complement of a binary number is a two stage process:

  1. calculate the one's complement
  2. add one

One's Complement

The one's complement of a binary number is created by flipping all of the bits - i.e. changing 0s to 1s and 1s to 0s. This can be done in a number of ways, including by using a bitwise NOT on the original binary number.

For example, if we're using four bits, the binary representation of 5 would be 0101, and the one's complement of that would be 1010.

Two's Complement

The two's complement is the result of a binary addition of the one's complement and the digit 1.

If the one's complement of 0101 is 1010, then the two's complement is 1011. Note that the usual rules of addition apply, so if the least-significant bit of the one's complement is 1, then you'll need to "carry".

As two's complement can be used to represent negative numbers, this means that the four-bit binary representation of 5 is 0101, and -5 would be 1011.

Number Line

Why do we need to add one to get the negative representation of a binary number - why isn't it just the one's complement?

You're probably familiar with the number line from your maths lessons. If you're including negative numbers, then zero is in the middle, e.g.

Denary number line

Note that there's a kind of symmetry, e.g. 1 and -1 are both the same distance from 0, as are 2 and -2, 3 and -3, etc.

Now let's look at a binary number line, where the negative numbers are represented using two's complement:

Binary two's complement number line

There's still a sort of symmetry, but this time the reflections are one's complements of each other, and the line of symmetry isn't through zero, it's between 0 and -1. This means that they're shifted over slightly - the sort of effect you could reproduce by adding one.

In fact the same thing happens with the denary numbers. You probably don't notice it with the binary numbers because there are only possible two digits, but the "complement" is like a "number bond" that you learn about in maths. With the three-digit example above, you can see what happens below zero if you subtract the number from 999 and then add one - e.g. subtracting 1 from 999 gives 998, and then adding 1 gives 999, which is the first number below zero.

Why Use Two's Complement?

You might remember from your maths lessons that subtracting 5 is the same as adding -5. This means that you can subtract a number in binary by performing an addition with it's two's complement - that will work, and it will give you the correct answer. In the example below we're subtracting 5 from 6 by adding -5 - you simply discard any overflow bits:

Binary subtraction by adding two's complement

This means that addition and subtraction can both performed in the ALU by the same circuit - e.g. a full adder - thereby reducing the complexity of the processor.