Subject: |
Two's Complement |
Author: |
James K. Smythe |
Date: |
19 Feb 1999 |
Advangtage: |
The same electronic circuitry can be used for both addition and subtraction |
Consider a string of 8 bits. The range of possible signed integer values can go from -256 to +255. See Table 1.
Table 1
Decimal |
Binary(2's Complement) |
Hex |
-256 |
1000 0000 |
80 |
-1 |
1111 1111 |
FF |
0 |
0000 0000 |
00 |
1 |
0000 0001 |
01 |
255 |
0111 1111 |
7F |
Lets review 2's complement of an 8 bit number. 2's complement is used to represent a signed positive or negative quantity. For a positive quantity, the first bit will be a '0' digit. The value will be the same as for an unsigned string of bits, i.e. no further conversion is necessary.
For a negative quantity, the first bit will be a '1' digit. The following steps illustrate the conversion process to obtain the negative value(decimal to 2's complement). Suppose we want to represent a value of -12d using 2's complement. First we find the positive value:
12d = 0000 1100b
By changing all zeros to ones and all ones to zeros, we compute its 1's complement: 1111 0011b
By taking the 1's complement and adding 1 (0000 0001b), we obtain the 2's complement: 1111 0100b
To go from 2's complement to decimal, take the 2's complement again!
1111 0100b = - 0000 1100b = -12d
Alternate Method:
An alternative method, that I prefer as a shortcut, is to do the following:
Starting from the leftmost bit of the positive binary number, flip(change 0 to 1 and 1 to 0) each bit until you reach the last '1' bit. Leave the trailing zeros to the right unchanged. See table 2.
Table 2
Decimal |
Binary |
2’s Complement |
-12 |
-(0000 1100) |
1111 0100 |
-1 |
-(0000 0001) |
1111 1111 |
-256 |
-(1000 0000) |
1000 0000 |
Notice that for -256d, the 2's complement is the same as the positive representation with a negative sign. Of course you would need an extra ninth bit for the sign field. This accounts for the assymetric range of signed integers that can be represented. Note that the smallest negative number is -256d while the largest positive number is +255d. Effectively the eigth bit is used for the 2's complement representation and therefore limits the largest positive value to 0111 1111b or +255d.
Of course this seems like so much fuss to do, however the same electronic circuitry can be used for both addition and subtraction. Adding the 2's complement is identical to subtracting the positive value. We can then use the some array of gates to perform both operations and save on IC chip real estate (reduce cost).