Two's Complement and Binary Subtraction
So far every binary number has been positive. To store negative numbers, the 256 patterns of an 8-bit register have to be shared between positive and negative values. One’s complement is the obvious first attempt and it has two flaws. Two’s complement fixes both, and it lets a computer subtract by adding. There is also a shortcut that turns the two-step conversion into one pass.
1. The problem: where does the minus sign go?
A register holds only 0s and 1s. There is no space for a “−” symbol, so the sign has to be shown by the bits themselves. Both systems below use the same idea: if the most significant bit (MSB) is 1, the number is negative. They differ in how the remaining bits are worked out.
2. One’s complement
It is easy to do, but it causes two problems:
Two zeros
0000 0000 is +0, and inverting it gives 1111 1111, which is −0. One of the 256 patterns is wasted on a duplicate. Every comparison with zero also has to check for both patterns.
Range in 8 bits: −127 to +127.
Addition needs a fix-up step
When adding a negative number produces a carry out of the MSB, that carry has to be added back on at the right-hand end (an end-around carry) before the answer is correct. That means a second addition, and extra circuitry.
3. Two’s complement
Adding 1 after inverting removes the duplicate zero. Inverting 0000 0000 gives 1111 1111, and adding 1 gives 1 0000 0000. The 9th bit is discarded, leaving 0000 0000 again. There is now only one zero, so the spare pattern holds an extra negative number: 1000 0000 is −128.
| 8-bit pattern | Unsigned | One’s complement | Two’s complement |
|---|---|---|---|
0111 1111 | 127 | +127 | +127 (largest) |
0000 0001 | 1 | +1 | +1 |
0000 0000 | 0 | +0 | 0 |
1111 1111 | 255 | −0 | −1 |
1111 1110 | 254 | −1 | −2 |
1000 0001 | 129 | −126 | −127 |
1000 0000 | 128 | −127 | −128 (smallest) |
Converting denary to 8-bit two’s complement
- Write the positive version of the number in 8-bit binary, padded with leading 0s. (If the number is positive, stop here. The MSB must be 0.)
- For a negative number, invert every bit.
- Add 1. Ignore any carry out of the MSB.
Converting 8-bit two’s complement to denary
- MSB is 0: the number is positive. Read it as ordinary binary.
- MSB is 1: the number is negative. Either add up the place values using −128 for the MSB (Figure 1), or find its two’s complement again to get the positive size, then put a minus sign in front.
4. The shortcut
Look closely at what happens when you invert and then add 1. The figures below find −104 both ways.
The observation: the original number ends in 1000, its first 1 from the right followed by zeros. Inverting turns that into 0111. Adding 1 to 0111 carries all the way through the 1s and stops at the 0, giving 1000 again. So the bits up to and including the first 1 come out unchanged, and the carry never reaches the bits to their left, which stay inverted. That is true for any number, which gives the shortcut:
It works both ways
Apply it to a negative number to get the positive one back. 1110 1110 → copy 10, invert the rest → 0001 0010 = 18, so the original was −18.
Edge case: zero
0000 0000 has no 1 to stop at, so every bit is copied and the result is still 0. There is no −0, as expected.
Edge case: −128
1000 0000 → copy 1000 0000 (the first 1 is the MSB) → unchanged. +128 does not exist in 8 bits, so −128 has no positive partner.
5. Binary subtraction using two’s complement
A processor does not need a separate circuit for subtraction. Taking away a number is the same as adding its negative:
The carry out of bit 7 is discarded. It is not an error. With two’s complement a carry out of the MSB is a normal part of adding a negative number.
The answer 1110 0010 starts with 1, so it is negative. To read it, use the shortcut: copy 10, invert 111000 to 000111, giving 0001 1110 = 30. The answer is −30 ✓.
Overflow in two’s complement
The result of a subtraction must still fit in the range −128 to +127. If it does not, the answer is wrong, and a carry out tells you nothing about it. Example 1 and Example 3 both had a carry out and both answers were correct.
6. One’s complement vs two’s complement
| One’s complement | Two’s complement | |
|---|---|---|
| To negate | Invert every bit | Invert every bit, then add 1 (or use the shortcut) |
| MSB place value | −127 | −128 |
| 8-bit range | −127 to +127 | −128 to +127 |
| Representations of zero | Two (+0 and −0) | One |
| Carry out of the MSB | Must be added back on (end-around carry) | Discarded |
| Used in modern computers? | Rarely | Almost universally for signed integers |
7. Check yourself
Convert −45 to 8-bit two’s complement.
45 = 0010 1101. Using the shortcut: the first 1 from the right is the LSB, so copy just that 1 and invert the other seven bits: 1101 0011. Check: −128 + 64 + 16 + 2 + 1 = −45 ✓.
1011 0100 is an 8-bit two’s complement integer. Convert it to denary.
The MSB is 1, so it is negative. Place values: −128 + 32 + 16 + 4 = −76. Check with the shortcut: copy 100, invert 10110 to 01001, giving 0100 1100 = 76, so the original was −76 ✓.
Use the shortcut to find the two’s complement of 0101 0000.
Copy up to and including the first 1 from the right: 10000. Invert the remaining 010 to 101. Result: 1011 0000. Check: −128 + 32 + 16 = −80, and 0101 0000 is 80 ✓.
Explain why one’s complement has two representations of zero, and why this is a disadvantage. [2]
Negating a number in one’s complement inverts every bit, so +0 (0000 0000) becomes 1111 1111, a second pattern meaning −0. This wastes one of the 256 patterns, so the range is only −127 to +127. It also means a test for zero has to check for two different patterns.
Calculate 37 − 52 using 8-bit two’s complement. Show your working.
37 = 0010 0101. 52 = 0011 0100, so −52 = 1100 1100 (copy 100, invert the rest). Add: 0010 0101 + 1100 1100 = 1111 0001, with no carry out. The MSB is 1, so the answer is negative. Copy 1 and invert the rest to get 0000 1111 = 15. The answer is −15 ✓.
State the largest and smallest values in 8-bit two’s complement, and explain why the range is not symmetrical.
Largest +127 (0111 1111), smallest −128 (1000 0000). Half of the 256 patterns have an MSB of 0, and they cover 0 to +127. Zero uses one of them, leaving 127 positive numbers. The other 128 patterns all have an MSB of 1 and are all negative, covering −1 to −128.
Calculate 90 − (−50) in 8-bit two’s complement and comment on the result. [3]
Subtracting −50 is the same as adding +50: 0101 1010 + 0011 0010 = 1000 1100. The MSB is 1, so the stored result reads as −128 + 8 + 4 = −116. The correct answer is 140, which is greater than +127 and cannot be represented in 8 bits. Two positive numbers have given a negative result, so an overflow has occurred.