Binary Arithmetic
SUBTRACTION
Two’s Complement
Subtraction can be done in the same was as Base 10. However, it is easier to add the negative representation of the number.
0010 1010 – 0001 0011 is the same as 42 – 19 in decimal
ones complement of 0001 0011 is 1110 1100
twos complement of 0001 0011 is 1110 1100 + 1 = 1110 1101
|
|
0 |
0 |
1 |
0 |
1 |
0 |
1 |
0 |
+ |
|
|
|
1 |
1 |
1 |
0 |
1 |
1 |
0 |
1 |
|
|
|
1 |
0 |
0 |
0 |
1 |
0 |
1 |
1 |
1 |
|
|
We ignore the 1 overflow. 10111 is 23 in decimal. 42 – 19 = 23.
If we do the subtraction in reverse we should get a negative number.
0001 0011 - 0010 1010 is the same as 19 – 42 in decimal
ones complement of 0010 1010 is 1101 0101
twos complement of 0010 1010 is 1101 0101 + 1 = 1101 0110
|
|
0 |
0 |
0 |
1 |
0 |
0 |
1 |
1 |
+ |
|
|
|
1 |
1 |
0 |
1 |
0 |
1 |
1 |
0 |
|
|
|
|
1 |
1 |
1 |
0 |
1 |
0 |
0 |
1 |
|
|
We need to undo the twos complement to get the answer we are expecting.
Firstly, subtract 1
1110 1001 - 1 = 1110 1000
Reverse ones complement
1110 1000 now becomes 0001 0111 and this is the number 23 so in decimal 19 – 42 = -23