Negative Numbers and Two's Complement
See why computers store negative integers with two's complement, convert numbers to and from it, and learn the range of signed 8-bit values.
Welcome back!
Step 1 of 12Learn
Where does the minus sign go?
So far every binary number has been zero or positive. Those are called unsigned integers. But computers also need negative numbers: temperatures below zero, bank withdrawals, a character moving left. Bits have no minus sign, so how do you store −5?
A first idea: use the leftmost bit as a sign bit, 0 for positive and 1 for negative, and the rest for the size. So +5 would be 00000101 and −5 would be 10000101. This is called sign-magnitude, and it has two annoying problems. First, there are two zeros: 00000000 (+0) and 10000000 (−0). Second, ordinary addition doesn't work: adding 00000101 (+5) and 10000101 (−5) with the normal rules gives 10001010, which would mean −10, not 0. The hardware would need special circuits for negative numbers.
Almost every modern computer instead uses two's complement. It's slightly less obvious at first, but it has one zero, and the same addition circuit works for positive and negative numbers alike. That simplicity in hardware is why it won.
The key idea: in two's complement, the leftmost bit has a negative place value. In 8 bits, the places are:
| −128 | 64 | 32 | 16 | 8 | 4 | 2 | 1 |
|---|
Everything else is ordinary binary.
Step 2 of 12Practice · Diagram
Place values of a signed byte
Tap the place that makes a pattern negative when it is 1.
Tap the shape that answers the question, then press Check.
- 8-bit two's complement
- −128
The leftmost place is negative.
- 64
- 32
- 16
- 8
- 4
- 2
- 1
Step 3 of 12Practice · Work it out
In 8-bit two's complement, the leftmost place is worth −128. What decimal value does 10000011 represent?
Step 4 of 12Learn
Range of signed numbers
With the leftmost place worth −128, the smallest 8-bit value is 10000000 = −128 (only the negative bit on). The largest is 01111111 = 64 + 32 + 16 + 8 + 4 + 2 + 1 = 127 (everything except the negative bit).
So a signed byte holds −128 to 127, while an unsigned byte holds 0 to 255. Both have exactly 256 values; they just cover different ranges. The pattern 11111111 means 255 unsigned but −1 signed (−128 + 127).
In general, n bits in two's complement represent −2^(n−1) to 2^(n−1) − 1:
| Bits | Signed range |
|---|---|
| 8 | −128 to 127 |
| 16 | −32,768 to 32,767 |
| 32 | about −2.1 billion to 2.1 billion |
The range isn't symmetric: there's one more negative number than positive, because zero takes one of the non-negative patterns.
A quick way to read the sign: if the leftmost bit is 0, the number is zero or positive and you read it normally. If it's 1, the number is negative.
Overflow happens here too, and it looks dramatic: in a signed byte, 127 + 1 wraps around to −128. Some games have shown characters suddenly getting huge negative stats, or vice versa, for exactly this reason.
Step 5 of 12Practice · Diagram
The signed 8-bit number line
Tap the pattern that represents −1.
Tap the shape that answers the question, then press Check.
- 10000000 = −128
-128
- 11111111 = −1
-1
- 00000000 = 0
0
- 01111111 = 127
127
Step 6 of 12Practice · Sort into groups
In 8-bit two's complement, is each value positive (or zero) or negative?
Tap an item, then tap its group (or drag it there).
Step 7 of 12Learn
Making a number negative
To find the two's complement representation of a negative number, use the flip and add one rule:
- Write the positive number in binary, with all 8 bits.
- Flip every bit (0 becomes 1, 1 becomes 0).
- Add 1.
Example: represent −5.
- +5 =
00000101 - Flip:
11111010 - Add 1:
11111011
Check with place values: −128 + 64 + 32 + 16 + 8 + 2 + 1 = −128 + 123 = −5. It works.
The same rule works in reverse: to find out which negative number a pattern represents, flip and add one to get its positive size. For 11111011: flip → 00000100, add 1 → 00000101 = 5, so the original was −5.
Now the magic. Add +5 and −5 with the ordinary addition rules:
00000101 (+5)
+ 11111011 (−5)
----------
100000000Drop the ninth bit, as an 8-bit computer would, and the result is 00000000: exactly zero. Subtraction becomes "add the negative," using the very same adding circuit. That's the reason two's complement is used almost everywhere.
Step 8 of 12Diagram
Flip and add one: writing −5
Tap a shape with to see more.
- 00000101
+5 in 8 bits.
- Flip: 11111010
Every 0 becomes 1 and every 1 becomes 0.
- Add 1: 11111011
- Check: −128 + 123 = −5
64 + 32 + 16 + 8 + 2 + 1 = 123.
Step 9 of 12Practice · Put in order
Put the steps for writing −12 in 8-bit two's complement in order.
Drag the items, or use the arrow buttons, to put them in order.
- Flip every bit to get 11110011
- Add 1 to get 11110100
- Check that −128 + 64 + 32 + 16 + 4 equals −12
- Write +12 in 8 bits as 00001100
Step 10 of 12Note
Step 11 of 12Practice · Code challenge
Write signedValue(bits) that takes an 8-character string of 0s and 1s and returns its value as an 8-bit two's complement number. For example, signedValue("11111011") returns -5 and signedValue("01111111") returns 127.
signedValue("11111011")signedValue("01111111")signedValue("10000000")signedValue("11111111")signedValue("00000101")
Step 12 of 12Practice · Choose one
A signed 8-bit variable holds 127. What does it hold after adding 1, if it wraps around?
Lesson complete!