Learn
1.1.6 | NEGATIVE NUMBERS AND TWO’S COMPLEMENT
01 | HOW CAN BITS REPRESENT A NEGATIVE VALUE?
So far, an unsigned 8-bit pattern has represented a value from 0 to 255. But programs also need values below zero, such as a temperature of −5 or a negative change in a balance.
Two’s complement is a system for representing signed integers. The same bits can mean different values depending on the representation being used. You must know whether the question uses unsigned binary or two’s complement.
In this lesson we use exactly eight bits. The leftmost bit has a negative place value; the other seven keep their ordinary positive place values.
02 | THE LEFTMOST PLACE IS WORTH NEGATIVE 128
| Stage | -128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Example: −10 | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 0 |
11110110 = −128 + 64 + 32 + 16 + 4 + 2 = −10
A 1 in the first column contributes −128. It is not simply a minus sign followed by a seven-bit magnitude. Here, the remaining positive contributions total 118, giving −128 + 118 = −10.
A leading 0 means zero or a positive value. A leading 1 means a negative value in this eight-bit two’s complement system.
03 | THE RANGE IS −128 TO +127
| Stage | -128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Largest positive | 0 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
| Zero | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| Most negative | 1 | 0 | 0 | 0 | 0 | 0 | 0 | 0 |
| Negative one | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
The greatest positive value is 64 + 32 + 16 + 8 + 4 + 2 + 1 = 127. The most negative value is −128 with no positive contributions.
There are still 256 possible patterns: 128 negative values, zero and 127 positive values. Two’s complement has one representation of zero.
Unsigned 8-bit binary ranges from 0 to 255. Eight-bit two’s complement ranges from −128 to +127. The width is the same; the interpretation is different.
04 | POSITIVE VALUES KEEP THEIR FAMILIAR FORM
| Stage | -128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| +10 | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 |
| +37 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 1 |
For values from 0 to 127, use the ordinary binary pattern padded to eight bits. The leading bit is 0.
Do not invert and add one when the question asks for a positive value. Those steps below construct the corresponding negative representation.
05 | BUILD A NEGATIVE VALUE: INVERT, THEN ADD ONE
To represent −10, start with the eight-bit pattern for +10. Change every 0 to 1 and every 1 to 0, then add 1 using binary addition.
| Stage | -128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| +10: starting value | 0 | 0 | 0 | 0 | 1 | 0 | 1 | 0 |
| Invert every bit | 1 | 1 | 1 | 1 | 0 | 1 | 0 | 1 |
| Add this value | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| −10: final pattern | 1 | 1 | 1 | 1 | 0 | 1 | 1 | 0 |
Inverting alone is not enough. 11110101 represents −11 in this system. The final addition produces 11110110, which represents −10.
Keep all eight bits throughout the method. Show the starting pattern, inversion and final answer clearly.
06 | ANOTHER EXAMPLE: NEGATIVE 37
| Stage | -128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| +37 | 0 | 0 | 1 | 0 | 0 | 1 | 0 | 1 |
| Invert | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 0 |
| Add one | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| −37 | 1 | 1 | 0 | 1 | 1 | 0 | 1 | 1 |
Check the signed place values: −128 + 64 + 16 + 8 + 2 + 1 = −37
Use a different method to check your answer: read the negative place value and add the positive columns containing 1. This catches mistakes in either the inversion or the addition.
07 | READ A NEGATIVE PATTERN USING PLACE VALUES
| Stage | -128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Given pattern | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 0 |
The leading bit is 1, so include −128. The other 1s are in the 32, 16 and 4 columns.
10110100 = −128 + 32 + 16 + 4 = −76
This method works for any eight-bit two’s complement pattern, including the special case 10000000.
08 | OR FIND THE MAGNITUDE, THEN APPLY THE MINUS
For a negative pattern, you can invert and add one to find its magnitude, then attach a minus sign to the denary answer.
| Stage | -128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Given: −76 | 1 | 0 | 1 | 1 | 0 | 1 | 0 | 0 |
| Invert | 0 | 1 | 0 | 0 | 1 | 0 | 1 | 1 |
| Add one | 0 | 0 | 0 | 0 | 0 | 0 | 0 | 1 |
| Magnitude: 76 | 0 | 1 | 0 | 0 | 1 | 1 | 0 | 0 |
01001100 is 76, so the original value is −76. Do not forget the minus sign. Use this method only after establishing that the original pattern is negative.
09 | HANDLE NEGATIVE 128 CAREFULLY
−128 is represented by 10000000. There is no +128 in eight-bit two’s complement: the greatest positive value is +127.
If you invert 10000000 and add one, you get 10000000 again in eight bits. Read that result as an unsigned magnitude of 128 when finding the magnitude, not as a positive signed value in the same eight-bit system.
The clearest check is the place-value method: −128 plus no positive contributions is −128.
10 | THE SAME PATTERN CAN HAVE TWO INTERPRETATIONS
| Stage | -128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|---|---|---|---|---|---|---|---|
| Pattern | 1 | 1 | 1 | 1 | 1 | 1 | 1 | 1 |
Unsigned: 128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = 255 Two’s complement: −128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = −1
The bits have not changed. The assigned value of the leftmost column has changed. This is why a question must specify the representation.
Two’s complement also supports signed arithmetic, but signed overflow is not judged by the same rule as the unsigned overflow in 1.1.4. Do not assume any carry out always means signed overflow.
11 | TRY IT: READ ANY EIGHT-BIT PATTERN
Enter a pattern and predict its signed value. Compare the signed and unsigned interpretations and inspect which columns contribute.
PRACTISE | BUILD AND EXPLAIN
For the guided builder, choose a magnitude from 1 to 127 and reveal the steps in order. Then complete the activities and independent questions.