5. Twos complement

Adding a positive number and a negative number in binary can be awkward (even for a CPU), but using the twos complement method makes it quite straightforward.

Example

Representing -5 in an 8 bit 2's complement format, carry out the following steps

1. Represent the number as if it was a positive 8 bit number, so this is 00000101

2. Keep the rightmost 1 but then flip all the bits to the left of it like this

00000101

keep the rightmost 1 and flip the rest

11111011

So 11111011 is -5 in twos complement format

To show how you can add 7 and -5 together the answer should be 2. So let's do it. +7 is 00000111

 00000111 
11111011

----------
100000010

The pink 1 is in the 9th position, and so in an 8 bit register it is simply discarded. So you end up with 00000010 which is 2 denary.

Example 2

Add -5 and -6 together using two's complement. The answer should be -11.

1. Convert -6 into 2's complement

+6 = 00000110
hold on to the rightmost 1 and flip the rest
-6 = 11111010

2. As shown above -5 in 2's complement is 11111011

3 Add the two numbers together

11111010

11111011

----------
111110101
Is this the right answer? Lets work out -11 in twos complement
+11 = 00001011
hold on to the rightmost 1 and flip the rest
-11 = 11110101
Which is the right answer.    


Example 3

What does a twos complement 1001 represent as a denary number?

1. Work out what the positive number is by holding on to the rightmost 1 then flipping the bits to the left of it, so it becomes 0111 which is 7. So twos complement 1001 is -7.

     
 

Copyright © www.teach-ict.com