Custom Search

Two's Complement

The two's complement system is the most common method of representing signed (+ or -) integers on a computer.

A two's-complement system, or two's-complement arithmetic, is a system in which negative numbers are represented by the two's complement of the absolute value.

The two's complement of the number behaves like the negative of the original number in most arithmetic, and it can coexist with positive numbers in a natural way.

The two's complement of a binary number is obtained by considering the MSB as negative and the rest of the bits as positive. For an N-bit number the two's complement version of it would have the 2N bit as a negative value - all of the others would be positive; summing them would then give you a negative value for the binary number.

For example: if we have to find the N's complement of 0110 (decimal 6) it would be 1010. Let me show you why.

The MSB (most significant bit) would be the negative value:

1010 = (in decimal)= -23 + 21 = -8 +2 = -6

How to change a binary number to two's complement

In order to change a number to two's complement you have to:

      • 'flip' all of the digits (high to low and vice versa) and then
      • add '1'.

To use the example above we would have 0110 flipped to 1001 and then we would have to add 1:

1001 + 0001 = 1010

To find out how to work out a two's complement in an even quicker, easy way click here.

Normal binary addition

Normal binary addition works as if two numbers were unsigned numbers.

  • If there is a carry bit beyond the end of the word, ignore the carry.
  • If the result is larger than can be held in the word size, overflow occurs

Binary subtraction

Binary subtraction is achieved using addition

You find the Two's complement of the subtrahend (the number you are going to 'take away') and add it to the minuend (the number you were going to take away from!).

In other words

a - b = a + (-b)

Example sum:

Let's do a really simple sum in binary. Add decimal 3 to decimal -5.

3 in binary is 0011

-5 in binary can be worked out using two's complement. Remember that the MSB is the sign bit!

0101 is +5 in binary. To change it to the equivalent of -5 we switch digits and add 1. We therefore get 1011

Okay, now, to do the subtraction sum '3 - 5'

we actually perform the addition sum '3 + (-5)'

In your head you know that the answer to our simple sum is -2... but the negative numbers look strange to us. Is 1110 really -2?

To find out we need to find the positive equivalent of it.

  • We can do this by reversing the steps above. If we do this we get 0010 - which we recognise as binary 2 Therefore we got it right!
  • OR we can add up the values for each digit MSB = -8; others are positive +4 + 2 = +6. Therefore 1110 = -8 + 6 = -2

Ignore end-around carry

The MSB is the sign bit - therefore a carry into an even-more significant bit would not make sense! If that occurs you just ignore it!

For example Let's add two 8-bit numbers using two's compliment: +106 and -2

Ignoring the end-around-carry we get an answer of 0110 1000 which is decimal 104