Integers : Two's Complement

To represent negative integers, a method called two's complement is used.

Example

To represent the number -13 as an 8-bit binary number...

Step 1 : Convert the positive number 13 into an 8-bit binary integer:

128 64 32 16 8 4 2 1
0 0 0 0 1 1 0 1

Step 2 : Change all the bits from 0 to 1 (or from1 to 0.)

128 64 32 16 8 4 2 1
0 0 0 0 1 1 0 1
1 1 1 1 0 0 1 0

Step 3 : Add 1 - Perform a binary calculation.

1 1 1 1 0 0 1 0
            + 1
1 1 1 1 0 0 1 1

Remember when adding in binary that 1 + 1 = 10 etc...

Final answer : -13 in binary (using two's complement notation) is 11110011

 

A strange system????

Yes...but it works! Consider the first example .......

The number 13 is represented as 00001101

and -13 is represented as 11110011

What do we get if we add these two numbers together (we should get 0)?

0 0 0 0 1 1 0 1
1 1 1 1 0 0 1 1
0 0 0 0 0 0 0 0

 

 

Another problem.....

How does the computer know if a number is positive or negative?

Does 11110011 represent -13 or does it represent (128+64+32+16+2+1) = 243?

The usual system is to say that the first (left) bit represents a sign bit (0 means the number is positive and 1 means it is negative).