Two's Complement Clinic
Store negative numbers in binary. Master the sign bit, the flip-and-add-1 method, and converting signed 8-bit numbers both ways.
Get the method right under pressure
Free interactive practice on the steps that lose marks under exam pressure.
Start revising freeWhat you'll cover
Storing negatives
Plain binary can only count upward from zero, but computers need negative numbers too. The standard solution is two's complement, and it is a genuinely clever one. By the end of this module you will have added a positive and a negative number together using nothing but ordinary binary addition, watched the answer come out right, and seen what happens when a program runs out of bits and quietly turns 200 into a negative number.
The words for it
Five terms. The second one is the whole idea in a single line:
How many values?
An 8-bit two's complement number runs from −128 to +127. How many DIFFERENT values can it represent altogether? (Do not forget zero.)
Flip and add 1
To write a negative number in two's complement, three steps:
1. Write the positive value in 8-bit binary.
2. Flip every bit: 0 becomes 1, 1 becomes 0.
3. Add 1 to the result.
Worked on −20: +20 is 0001 0100. Flipping gives 1110 1011. Adding 1 gives 1110 1100.
Check it by adding the columns: −128 + 64 + 32 + 8 + 4 = −20. If your check does not come out to the number you started with, you have flipped a bit wrong, and it is worth doing every time.
Encode a negative
Write −5 as an 8-bit two's complement number. Enter the 8 bits with no space, for example 10101010.
Two ways to decode
Given 1110 1100, whose leading 1 tells you it is negative, there are two routes to the answer and both are worth having:
Decode it
Convert the 8-bit two's complement number 1101 0011 to denary. (Include the minus sign if it is negative.)
Why it wins
Two's complement is the standard for two reasons, and the second is the one worth seeing rather than being told.
First, there is exactly one code for zero, 0000 0000. Schemes that store a sign separately end up with both a positive and a negative zero, which then have to be special-cased everywhere.
Second, ordinary binary addition just works. Add +5 and −5:
``
0000 0101 (+5)
+ 1111 1011 (−5)
-----------
1 0000 0000
`
The answer is nine bits long, but the machine only has eight. Throw the ninth away and you are left with 0000 0000`, which is zero, which is correct. No subtraction circuit, no checking of signs, no special cases: the same adder that handles positive numbers handles negative ones for free. That is the whole reason this representation won.
The ninth bit
Adding +5 and −5 in 8-bit two's complement produced a nine-bit result, and the leading 1 was thrown away. Why is discarding it correct rather than a fudge?
- The eight columns already run from −128 upwards, and the ninth carry represents exactly the 256 that the wrap-around accounts for, so dropping it lands on the right answer rather than approximating it
- The ninth bit is the least significant, so it makes almost no difference to the answer
- It is a rounding error, and the answer is only approximately zero
- It is a spare sign bit, and it is dropped because the answer is positive
When 200 goes negative
A program stores signed values in single 8-bit registers. Something has gone wrong. Four questions.
- The program adds 100 and 100 and prints −56. The addition circuitry is working perfectly. What has happened?
- How could the program detect that this had happened, without knowing the answer in advance?
- What is the straightforward fix?
- Could the same thing happen when adding a positive number and a negative one?
Which are true?
Select ALL THREE statements that are TRUE.
- Two's complement means a processor needs only one adder circuit, because subtracting is adding a negative
- Adding two positive numbers and getting a negative result is a reliable sign that overflow has occurred
- There is exactly one bit pattern for zero, which is why no special case is needed for it
- All eight columns hold positive place values
- The 8-bit range is symmetrical, running from −127 to +127
- Two's complement has two different codes for zero
The four corners
- 0111 1111
- 1000 0000
- 1111 1111
- 0000 0000
- 127, the largest value that fits
- −128, the smallest, and the only one with no positive counterpart
- −1: every column set, which surprises people
- 0, and the only pattern that means it
Two's complement summary
In two's complement the leftmost bit is the _____ bit, and its place value is _____. An 8-bit range runs from −128 to +127, which is 256 different values. To write a negative number, write the positive, _____ the bits, then add 1. The scheme is used because there is one code for zero and because ordinary binary _____ works for negatives without any special hardware.
Why not just a sign bit?
Exam practice. In about 60 words, explain why computers use two's complement rather than simply storing a sign in one bit and the size in the other seven. Include:
- what goes wrong with zero in the simpler scheme
- what two's complement lets a processor do with ordinary addition
- why that matters for the hardware a processor needs