Binary digits are just 0s and 1s – so how does a computer represent −5? The answer used by virtually every modern processor is two’s complement, a clever encoding where addition, subtraction, and sign handling all work with the same simple circuitry.
Why Not Just Use a Sign Bit?
The naive approach – “sign-magnitude” – reserves the leftmost bit as a sign flag: 0 for positive, 1 for negative. So +5 = `00000101` and −5 = `10000101`.
The problem: this creates two zeros (+0 = `00000000`, −0 = `10000000`) and requires special subtraction circuits. Two’s complement solves both problems: there is exactly one zero, and subtraction is just addition.
How Two's Complement Works
To find the two’s complement (negative) of a binary number:
- Invert all bits (0→1, 1→0) – this is the “one’s complement”
- Add 1 to the result
That’s it. The leftmost bit still indicates sign (0 = positive, 1 = negative), but the negative values are encoded this way.
Try it yourself: take any positive binary value, apply these two steps, and verify the result with a binary converter.
Worked Example: Representing −5 in 8 Bits
Start with +5 in 8-bit binary: `00000101`
Step 1 – invert: `11111010`
Step 2 – add 1: `11111011`
So −5 = 11111011 in 8-bit two’s complement.
Sanity check: as an unsigned number, 11111011 = 128+64+32+16+8+0+2+1 = 251. And 256 − 5 = 251. ✓ In two’s complement, −N is always encoded as 2ⁿ − N, where n is the bit width.
Worked Example: Representing −1
Start with +1: `00000001`
- Invert: `11111110`
- Add 1: `11111111`
So −1 = 11111111 – all ones. This is a useful pattern to recognize: in two’s complement, −1 is always all 1s regardless of bit width.
The Magic: Subtraction Becomes Addition
Here’s why hardware designers love two’s complement. To compute 7 − 5, the CPU actually computes 7 + (−5):
“`
00000111 (7)
+ 11111011 (−5)
——–
100000010
“`
Discard the carry bit beyond 8 bits → `00000010` = 2. ✓ The same adder circuit handles both addition and subtraction – no special logic needed.
One more: 5 − 7 = 5 + (−7). First find −7: 00000111 → invert → 11111000 → add 1 → `11111001`. Then:
“`
00000101 (5)
+ 11111001 (−7)
——–
11111110
“`
11111110: invert → 00000001, add 1 → 00000010 = 2, and the sign bit is 1, so the answer is −2. ✓
Range of Two's Complement Values
With n bits, two’s complement covers −2ⁿ⁻¹ to +(2ⁿ⁻¹ − 1):
| Bits | Minimum | Maximum |
|---|---|---|
| 8 | −128 | 127 |
| 16 | −32,768 | 32,767 |
| 32 | −2,147,483,648 | 2,147,483,647 |
| 64 | −9.22 × 10¹⁸ | 9.22 × 10¹⁸ |
Notice the asymmetry: there’s one more negative value than positive. The most negative value (−128 in 8 bits = `10000000`) has no positive counterpart – negating it overflows back to itself, a classic edge case in programming.
One's Complement vs Two's Complement
| One’s Complement | Two’s Complement | |
|---|---|---|
| Method | Invert bits only | Invert bits, then add 1 |
| Zero | Two zeros (+0, −0) | One zero |
| −5 (8-bit) | 11111010 | 11111011 |
| Hardware | Needs subtractor | Adder only |
One’s complement is simpler to compute by hand but the double-zero problem made it obsolete for integers. (It survives in IPv4 header checksums, interestingly.)
Detecting Overflow
Overflow happens when the true result doesn’t fit. In two’s complement, there’s a neat rule: overflow occurred if the carry into the sign bit differs from the carry out of it.
Practical version: adding two positives must give a positive; adding two negatives must give a negative. If 100 + 100 = −56 (in 8 bits: 01100100 + 01100100 = 11001000), that’s overflow – the real answer 200 exceeds the 127 maximum.
Frequently Asked Questions
What is the two's complement of 0?
00000000 → invert → 11111111 → add 1 → 100000000 → discard carry → 00000000. Zero is its own complement – which is exactly why there’s only one zero. ✓
Why is −128 = 10000000 in 8-bit two's complement?
Because 128 − 128 = 0 in the modular arithmetic: 10000000 + 10000000 = 100000000, discard carry → 00000000. It’s the one value with no positive counterpart since +128 doesn’t fit in 8-bit signed range.
How do I convert a negative two's complement number back to decimal?
If the sign bit is 1: invert all bits, add 1, and put a minus sign on the result. Example: 11110110 → invert → 00001001 → add 1 → 00001010 = 10 → answer −10.
What is the binary complement converter use case?
Converting between positive and negative representations when debugging embedded code, reading hex dumps of signed data, or understanding compiler warnings about signed/unsigned comparisons.
Does floating point use two's complement?
No – IEEE 754 floats use sign-magnitude (a dedicated sign bit) for the mantissa. Two’s complement is for integers. If you’re working with fractional binary values, a binary converter that handles the integer part can still help you verify bit patterns.