Signed binary and 2's complement
Humans use signed decimal notation for integers. We indicate whether a number is negative by preceding it with a minus sign. For instance, 837 is positive. (We could write +837, but a leading positive sign is usually omitted.) To make a number negative, just precede it with a minus sign: , for instance.
Integers could be written in signed binary notation. Just write the absolute value of the number in base two, and affix a negative sign if necessary. For example, 28 and in signed binary are
This has a lot of advantages, including convenience and clarity. However, it has some disadvantages, one of which is that subtraction becomes very messy. It requires borrowing, which is tricky and error prone. Just recall all the problems you had in grade school learning to borrow in subtraction!
There are other ways to represent negative integers, one of which is complement notation. This notation has the advantage of greatly simplifying subtraction.
Lets see how it works in base 10. The 10s complement of an integer is the difference between the number and a higher power of the base. For example, various complements of 837 are
1000 837 = 163
10,000 837 = 9,163
100,000 837 = 99,163
1,000,000 837 = 999,163
Obtaining a 10s complement by traditional subtraction is not pleasant. It requires borrowing in every position! Fortunately, there is an easier way.
- Fix the number of places, including at least one additional place on the left. Supply zeros in the extra places on the left.
- Replace each digit in the original number by 9 digit .
- Add 1 to the result.
For example, find the six digit 10s complement of 837.
- 000837
- 999162
- 999162 + 1 = 999163
Notice this is the same result we obtained above.
Heres why complement notation is nice. Consider the problem of subtracting 837 from 2146, say. Lots of borrowing is necessary.
Now try adding the complement of 837 instead of subtracting. Use six digits for convenience in all numbers, and throw away any carry out of the left most digit.
=>
which is the correct result. We can subtract by adding!
This scheme works for negative results. Consider 268 837 .
=>
The leading 9s are a signal that the number is negative. Convert it to signed notation by reversing the complementation (subtract 1 and replace each digit by 9 digit) or by complementing the complement and affixing a negative sign.
999431 =>
Sure enough, 268 837 = .
So far, we have seen the following equivalences.
Signed decimal 10s complement (six digits)
000163
999431
There isnt much use for 10s complement notation, except as a curiosity. However, suppose we use base 2 and 2s complement.
To convert a negative signed binary integer to 2s complement,
- Fix the number of bits. Write zeros in the extra places to the left.
- Replace each bit by 1 bit. Notice this amounts to reversing the 0s and 1s.
- Add 1, ignoring any carry out of the left most bit.
For example, find the 2s complement binary representation of using eight bits.
To find the representation, first convert 45 to binary.
Now pad with zeros on the left to make eight bits.
00101101
Switch 0s and 1s and add 1.
11010010
+ 1
11010011
To sum up,
Signed decimal Signed binary 2s complement binary (8 digits)
11010011
The 2s complement of a positive number is the same as the ordinary binary, with leading zeros affixed for emphasis.
Signed decimal Signed binary 2s complement binary (8 digits)
11010011
45 101101 00101101
Computers subtract by adding the 2s complement. Suppose we ask the computer to evaluate 85 38. In binary, using eight bits for convenience,
and ,
so
Finally,
In base 10, , which is the correct answer.
Computers use 2s complement notation for negative integers instead of signed binary. It has several advantages.
- It is easy to determine the sign of a 2s complement number: If it begins with 0, the number is positive; if it begins with 1, the number is negative.
- The programmer doesnt have to teach the computer to subtract by borrowing. Instead, it can complement and add.
- It is much simpler and less expensive to build an electronic circuit that can complement than it is to build one that can subtract by borrowing.
Exercises
1. Complete the table. The first two rows were done above.
Signed decimal |
Signed binary |
2s complement (8
bits) |
-45 |
-101101 |
11010011 |
45 |
101101 |
00101101 |
100 |
|
|
|
-1100001 |
|
|
|
00011001 |
|
1100 |
|
|
|
11000011 |
-66 |
|
|
0 |
|
|
|
1 |
|
-1 |
|
|
2. Complete the table.
Signed decimal |
Signed binary |
2s complement (8
bits) |
|
-1101100 |
|
17 |
|
|
|
|
01101100 |
|
100001 |
|
-58 |
|
|
|
|
10001001 |
In problems 3 through 10, do the decimal subtraction by using 10s complements, not by borrowing. Give the answers in signed decimal notation, however.
3. 37 12 4. 37 18
5. 37 37 6. 37 62
7. 37 78 8. 4285 1277
9. 462 498 10. 805 1486
In problems 11 through 16, do the binary subtraction by using 2s complements, not by borrowing.
11. 101 11 12. 1011 10
13. 1110 111 14. 1001 1100
15. 101 1101 16. 11011011 1101101
Will subtraction by adding the complement work if the first term is negative? In problems 17 through 20, do the arithmetic using 10s complements for all negative numbers, and check your answers using ordinary arithmetic.
17. -44 21 18. -108 97
19. -16 95 20. -16 209
21. What is the largest positive base 10 number that can be stored in 2s complement using 8 bits? What is the smallest negative number? Hint: Think about the bit patterns in base 2 and translate back to decimal.
22. What is the largest positive base 10 number that can be stored in 2s complement using 16 bits? What is the smallest negative number?
Credits and licensing
This article is by Robert P. Webber, licensed under a Creative Commons BY-SA 3.0 license.
Version 2015-Oct-5 13:40