Two’s Complement

Two’s complement is the way computers store integers so that it handles both positive and negative numbers.

The rule it follows to represent the inverse of a number is straightforward, you do the following operations on the bits:

So with decimal 3 being represented as 0011. To make this -3, we

We can revert back, too:

You might be ready to ask “but hold on 1101 represents decimal 13”! This statement is correct if you were talking about unsigned values. But when representing negative values, we call these ‘signed’, which means that the first bit is the sign, which indicates whether it is a positive or negative value. Therefore 01 indicates decimal 1 and 11 indicates decimal -1.

For this reason unsigned values can hold a larger positive number than signed values as they have one extra bit to work with. An unsigned byte can hold a value in the range of 0 to 255 whereas a signed byte can hold a value of -128 to 127.

So why do we do this? Why can’t we just use the sign and then the other bits using the standard value making 3 be 1011?

Because whilst this would make more sense in our head, using two’s complement allows the CPU’s circuitry for addition and subtraction to be the same operation - they are both adding by using basic carrying logic seen in circuits.