Negative numbers: two's complement
About this lesson
A byte is just 8 bits, with no room for a minus sign. So how does a computer store −5? Follow 5 as its bits are flipped and 1 is added to make −5, check the answer with place values, then add 5 and −5 to watch the carry fall off and leave exactly 0.
A byte holds 256 patterns
A byte is 8 bits, so it has 2 to the power 8 = 256 patterns. Read as plain binary, they mean 0 to 255. The back row holds 5: 00000101, which is the 4 bit plus the 1 bit.
>>> 2**8 # patterns in one byte 256 >>> 0b00000101 5
Read the lesson step by step
A byte holds 256 patterns
A byte is 8 bits, so it has 2 to the power 8 = 256 patterns. Read as plain binary, they mean 0 to 255. The back row holds 5: 00000101, which is the 4 bit plus the 1 bit.
>>> 2**8 # patterns in one byte 256 >>> 0b00000101 5A sign bit is not enough
First idea: let the top bit be a minus sign, so 10000101 means −5. This is called sign and magnitude. But it has two zeros, 00000000 and 10000000, and plain binary addition gives wrong sums. Computers use a cleverer rule.
>>> bin(0b00000101 + 0b10000101) # 5 + "-5" in sign and magnitude '0b10001010' >>> 0b0001010 # that answer would read as -10 10The top bit is worth −128
Two's complement keeps every place the same except the top one: it is worth −128 instead of 128. So 10000000 is −128, and 11111111 is −128 + 127 = −1. Small positive numbers keep their usual bits.
>>> -128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 # 11111111 -1 >>> -128 + 0 # 10000000 -128Step 1: flip every bit
To turn 5 into −5, first flip every bit: each 0 becomes 1 and each 1 becomes 0. 00000101 becomes 11111010, which is −6. Python's ~ operator does this flip: ~5 gives −6.
>>> ~5 -6 >>> bin(~5 & 0xFF) # the 8 flipped bits '0b11111010'Step 2: add one
Then add 1: 11111010 + 1 = 11111011, and that is −5. Why? A number plus its flipped copy is always 11111111, which is −1. So the flip is one short, and adding 1 fixes it.
>>> ~5 + 1 -5 >>> 5 + ~5 # a number plus its flip -1 >>> bin(-5 & 0xFF) '0b11111011'Check with place values
Check it with the place values. The 1 bits are worth −128, 64, 32, 16, 8, 2 and 1. Adding them gives −128 + 123 = −5. The top bit says negative; the rest say how far above −128.
>>> -128 + 64 + 32 + 16 + 8 + 2 + 1 -5 >>> 64 + 32 + 16 + 8 + 2 + 1 123Add 5 and −5
Now add 5 and −5 column by column, right to left, as on paper. 1 + 1 = 10 in binary: write 0, carry 1. Every column after that also makes 10, so the carry ripples out of the left end.
>>> bin(0b00000101 + 0b11111011) '0b100000000' >>> len("100000000") # one bit too many 9The extra carry falls off
The full answer, 100000000, needs 9 bits, but a byte has only 8. The extra carry falls off and is lost, leaving 00000000, which is 0. So 5 + (−5) = 0, using the same adder as for positive numbers.
>>> 5 + 251 # 251 is 11111011 read as plain binary 256 >>> (5 + 251) & 0xFF # keep only the low 8 bits 0From −128 to 127
Eight bits in two's complement hold −128 to 127: still 256 patterns, now half of them negative. The largest is 01111111 = 127, the smallest is 10000000 = −128. A top bit of 1 always means negative.
>>> -2**7, 2**7 - 1 (-128, 127) >>> (-5).to_bytes(1, "big", signed=True) b'\xfb' >>> int.from_bytes(b"\xfb", "big", signed=True) -5Seeing the bits in Python
Python integers have no fixed size, so −5 shows with a minus sign. To see its 8-bit pattern, keep the low 8 bits with & 0xFF: −5 & 0xFF is 251 (0b11111011). If it is 128 or more, subtract 256 to read it back.
>>> -5 & 0xFF 251 >>> bin(-5 & 0xFF) '0b11111011' >>> 251 - 256 -5
5 quick questions
Choose one answer per question. You can change your choices before checking them.
01In 8-bit two's complement, what is 11111111?
The top bit is worth −128 and the other seven bits add up to 127, so the total is −128 + 127 = −1.
02How do you write −3 in 8-bit two's complement? (3 is 00000011.)
Flip every bit to get 11111100, then add 1 to get 11111101. Check: −128 + 64 + 32 + 16 + 8 + 4 + 1 = −3.
03Why don't computers store negatives as a sign bit plus the size (sign and magnitude)?
00000000 is +0 and 10000000 is −0, so there are two zeros. And adding 00000101 (5) and 10000101 (−5 in sign and magnitude) bit by bit gives 10001010, which reads as −10, not 0.
04When the computer adds 00000101 (5) and 11111011 (−5), the answer has 9 bits: 100000000. What happens to the 9th bit?
A byte holds only 8 bits, so the carry out of the left end is lost. The 8 bits that remain are 00000000, which is 0, the right answer for 5 + (−5).
05In Python, what does -5 & 0xFF give?
& 0xFF keeps the low 8 bits of −5, which are 11111011. Read as plain binary that is 251, which is 256 − 5.
Glossary
- Two's complement
- The usual way computers store whole numbers that can be negative: the top bit has a negative weight (−128 in a byte) and every other place keeps its normal value.
- Sign and magnitude
- Storing a number as a sign bit plus its size. Simple, but it has two zeros and ordinary addition gives wrong answers.
- Sign bit
- The top (leftmost) bit. In two's complement a 1 there means the number is negative.
- Invert (flip)
- Turn every 0 into 1 and every 1 into 0. Python's ~ operator does this: ~x equals −(x + 1).
- Carry
- The 1 passed to the next column on the left when a column adds up to 2 or more, as in 1 + 1 = 10 in binary.
- Overflow
- When the true answer is outside the range the bits can hold, so the stored result is wrong. For example, 127 + 1 in a signed byte gives 10000000 = −128. The carry that falls off in 5 + (−5) is not overflow: the 8 bits left are the right answer.
- Mask
- A pattern used with & to keep some bits and clear the rest. x & 0xFF keeps the low 8 bits of x.
Notes and sources
- In sign and magnitude, 5 + "−5" is 00000101 + 10000101. Plain binary addition gives 10001010, which that scheme reads as −10, not 0.
- Why flip and add one works: x plus its flipped copy is always 11111111, which is −1. So the flip equals −x − 1, and adding 1 gives −x. Python defines ~x the same way, as −(x + 1).
- −128 has no positive partner in 8 bits: flipping 10000000 gives 01111111, and adding 1 gives 10000000 again.
- The same rule works for any width: n bits hold −2^(n−1) to 2^(n−1) − 1. A 16-bit integer holds −32,768 to 32,767, and a 32-bit one −2,147,483,648 to 2,147,483,647.
- The name: as plain binary, −x is stored as 2^8 − x, its complement with respect to 2^8 = 256. For 5 that is 256 − 5 = 251 = 11111011.
- Harris & Harris, Digital Design and Computer Architecture, 2nd ed., section 1.4.6 Signed Binary Numbers
- Bryant & O'Hallaron, Computer Systems: A Programmer's Perspective, 3rd ed., section 2.2.3 Two's-Complement Encodings
- MIT 6.004 Computation Structures notes: Basics of Information
- Cornell CS notes, Thomas Finley: Two's Complement
- Python library: Bitwise operations on integer types
- Python reference: Unary arithmetic and bitwise operations (~x)
Quiz Unlocks after the last step.