|
|
Subtraction using complements
It is a curious fact that a computer subtracts using addition.
First a method known as 2's Complement is used to create a negative
representation of the number to be subtracted..
Then subtraction is a matter of adding the negative number to the number from
which it is being subtracted. (!)
But before we look at 2's complement, let's use a familiar number system to
understand the concept of complements.
Complements
| The complement
of a number is the number which when added to the original will make it equal
to a multiple of the base number system. The complement of a number can be used as a representation of that number as a negative - moreover, as a positive number representing a negative! It is really a bit of a trick which we can use to make subtraction easier for machines. |
For example in the
decimal (base 10) system
the complement of 7 is 3 (i.e 7 + 3 = 10) |
Consider that:
|
|
7 minus 3 is mathematically the same as 7 plus (minus 3) i.e: 7 - 3 = 4 is the same as 7 + (-3) = 43 minus 3 is mathematically the same as 3 plus (minus 3) i.e: 3 - 3 = 0 is the same as 3 + (-3) = 0 |
7 minus 3 is the same as 7 plus the complement of 3 so: 7 - 3 becomes 7 + 7 which = 14If we remove the left-most 'additional' digit,
|
| 1) 7 -7 0 |
1a) 7 +3 1 0 |
! ! ! |
2) 99 -78 21 |
2a) 99 +22 1 21 |
! ! ! |
3) 2000 -1999 0001 |
3a) 2000 +8001 1 0001 |
! ! ! |
4) 100 - 7 93 |
4a) 100 + 3 103 |
! ! ! |
5) 100 -007 093 |
5a) 100 +993 1 093 |
In example 4a, the answer is incorrect! but as soon as we realise that in this system: the number we are subtracting must have the same number of digits as the number from which we are subtracting, then we can obtain a correct result. Study the examples 5) and 5a) above. Can you see why the complement of 007 is 993?
In the following examples, note that there is no 'discard' column - I have
put a '0' to denote this.
Can you see why the answers appear incorrect? and what needs to be done?
| 6) 1 -2 -1 |
6a) 1 +8 0 9 |
! ! ! |
7) 7 -17 -10 |
7a) 07 +83 0 90 |
! ! ! |
8) 11 -78 -67 |
8a) 11 +22 0 33 |
! ! ! |
9) 2000 -2999 - 999 |
9a) 2000 +7001 0 9001 |
All the results in the sums above should be negative. The results in the
sums using complements are negative but are still shown as complements
(i.e as positive representations of negative numbers). If we were using a
machine to do these calculations we could set it to note that there is no
discarded leftmost digit and that in order to return a 'true' negative number,
the result needs to be replaced by its complement with a minus sign in front.
So, in the examples above, '9' is
the complement form of '-1', '33' = '-67',
etc.
| Calculating the complement
of a binary number
1. Reverse the value of the
bits in the binary number; i.e. change all the 0 bits
to 1 and change 1s to 0.
(This is a shortcut - alternatively, you can look at the string
of bits and work out the complement as the binary number you would need to
add to obtain the next whole multiple of 2) Note that
in '2's complement' notation Positive numbers always start with '0' and
Negative numbers with '1' |
Getting 2's Complement - examples
| Step 1 | Step 2 | ||
|
|
00000001 becomes 11111110 |
11111110 +00000001 11111111 |
-1 = 11111111 |
|
|
00001100 becomes 11110011 |
11110011 +00000001 11110100 |
-12 = 11110100 |
|
|
00001101 becomes 11110010 |
11110010 +00000001 11110011 |
-13 = 11110011 |
|
|
01010111 becomes 10101000 |
10101000 +00000001 10101001 |
-87 = 10101001 |
|
|
01100100 becomes 10011011 |
10011011 +00000001 10011100 |
-100 = 10011100 |
You can use the above numbers to check out that adding negative binary numbers (i.e. complements) to positive binary numbers works as subtraction.
| For example, the decimal sum
'1 - 1', in binary representation: |
|
Note that: the additional lefthand bit flags whether the answer is positive (1) or negative (0) - as in the 'base 10' examples earlier.
Some examples of subtraction in binary using
2's complement
| a) 12 -2 10 |
01100 +11110 1 01010 |
not 1100 +1110 1 1010 |
! ! ! |
b) 87 -13 74 |
01010111 +11110011 1 01001010 |
! ! ! |
c) 1 -13 -12 |
00001 +10011 0 10100 |
not 0001 +0011 0 0100 |
! ! ! |
d) 87 -100 - 13 |
01010111 +10011100 0 11110011 |
| Tony's Home Page |