Two's Complement
Binary & Number Systems · Foundations track
6 min read
In this module
The Problem: Negative Numbers
Every binary value we've worked with so far has been positive. Real programs need to subtract, track debt, measure temperatures below zero, and a hundred other things that demand negatives. So how do you represent a minus sign using only 0 and 1?
First Attempt: Sign-Magnitude
The simplest idea: reserve the leftmost bit as a sign bit, 0 for positive and 1 for negative. The remaining bits hold the magnitude. So 0 0000101 is +5 and 1 0000101 is -5.
It works on the surface, but two serious problems lurk underneath:
- Positive zero and negative zero. Both
00000000(+0) and10000000(-0) represent zero, wasting a bit pattern and gumming up comparisons. - Addition breaks. Adding a positive and a negative with ordinary binary addition gives the wrong answer. You need extra circuitry to inspect signs and decide whether to add or subtract.
Sign-magnitude representation is intuitive but impractical. It produces two zeros and requires the CPU to handle addition and subtraction as separate operations.
Two's Complement
Nearly every modern computer uses two's complement instead. To negate a number in two's complement:
- Flip every bit (change each
0to1and each1to0). - Add 1 to the result.
Negate 5 (00000101):
- Flip:
11111010 - Add 1:
11111011
So -5 in 8-bit two's complement is 11111011. To verify, add 00000101 (5) and 11111011 (-5) with ordinary binary addition:
00000101+ 11111011---------- 100000000The result is 9 bits. Discard the extra bit and you get 00000000, exactly zero. The maths just works, no special cases needed.
The Range
In an 8-bit two's complement system, the leftmost bit still tells you the sign (0 = positive, 1 = negative), but the encoding parts ways with sign-magnitude.
- Largest positive value:
01111111= +127 - Smallest negative value:
10000000= -128 - Full range: -128 to +127, covering 256 values with no wasted patterns
You'll spot one more negative number than positive. That's because zero claims a slot on the positive side. There's only one zero in two's complement, which is the whole point.
The CPU does not need separate addition and subtraction circuits. Subtraction is just addition with a negated value. This simplifies hardware design and is one of the reasons two's complement became universal.
Worked Example: Reading a Negative Number
Say a memory dump shows the byte 11101100 and you know it's a signed 8-bit value. Two ways to decode it:
Flip and add 1. The top bit is set, so it's negative. Negate it to find out how negative: flip to 00010011, add 1 to get 00010100, which is 20. So the byte is −20.
Use a negative place value. This one's quicker once it clicks. In two's complement the top bit's place value is negative: −128 instead of +128. Everything else is the same as before. So 11101100 is −128 + 64 + 32 + 8 + 4 = −20. Same answer, no flipping.
The second method also explains the odd range. The biggest you can make is everything except the top bit: 64 + 32 + 16 + 8 + 4 + 2 + 1 = 127. The smallest is the top bit alone: −128.
Try It: Conversion Drill
Work each one out on paper before you open it.
Decode: the byte 0xF3 holds a signed 8-bit value. What number is it?
−13. 0xF3 is 11110011. With the negative place value: −128 + 64 + 32 + 16 + 2 + 1 = −13. Or flip to 00001100 and add 1 to get 00001101, which is 13.
Encode: what's −37 in 8-bit two's complement?
11011011, or 0xDB. 37 is 00100101. Flip to 11011010, add 1. Check it with place values: −128 + 64 + 16 + 8 + 2 + 1 = −37.
Add: what's 01100100 (100) plus 10011100 (−100) in 8 bits?
00000000. The full sum is 1 00000000, and the ninth bit falls off the end. The adder never knew one of the inputs was negative.
The program below prints each of those bytes with both of its readings, plus a couple of extras. You don't need to know C to use it: every show line takes a label and a byte, and (uint8_t)-37 means "the byte pattern for −37".
Sign Extension
What happens when an 8-bit −20 moves into a 16-bit register? You can't pad it with zeros, because 00000000 11101100 is +236. Instead the CPU copies the sign bit into all the new positions:
8-bit: 11101100 (−20)16-bit: 11111111 11101100 (still −20)That's sign extension, and x86 has dedicated instructions for it (movsx, cbw, cdqe and friends). Positive values are extended with zeros, negative ones with ones, and the value stays the same either way. Get this wrong, by zero-extending a signed value or sign-extending an unsigned one, and a small negative number turns into a large positive one. You'll see that bug again in Integer Overflows & Type Confusion.
Both 16-bit versions are below, read as signed. The first is the sign-extended 0xFFEC, which still says −20. The second is the zero-extended 0x00EC, which says 236. The low byte is identical in both; only the eight bits you added differ. Clear the top byte of the first one and you'll turn it into the second.
Predict: negate −128 in 8-bit two's complement. What do you get?
−128 again. 10000000 flips to 01111111, and adding 1 gives 10000000. There's no +128 in 8 bits, so the negation has nowhere to go. The same quirk exists at every width: in C, abs(INT_MIN) can't be represented and is undefined behaviour, and code that negates a value to "make it positive" before a bounds check can be fooled by exactly this one number.
To watch it happen, add int8_t m = -128; show("negated", (uint8_t)-m); to the drill program above.
Explore It
Use the visualiser below to toggle bits and watch the signed decimal value change. Pay attention to what happens when you flip the leftmost bit: turning it on subtracts 128 from the value, rather than simply changing the sign the way sign-magnitude would.
Two's complement looks like a parlour trick the first time you meet it. The more you use it, the more it reveals its quiet elegance. With this system in place, a CPU can handle positive and negative numbers using the same addition circuitry. A design choice that's held up for decades.