A computer stores every integer in a fixed number of bits, and bits have no minus sign. Two’s complement is the trick nearly every CPU uses to get negative numbers anyway: count backward from zero and see where the counter lands.
Picture an odometer with eight binary wheels. Counting down from 2 works as expected until it passes zero. The wheels have nowhere to go but back around to all ones, so that pattern is defined to mean −1.
00000010 2
00000001 1
00000000 0
11111111 -1 the wheels roll back past zero
11111110 -2
11111101 -3
Every pattern is a stop on one closed loop. The diagram shows the idea with 3 bits so it stays small; 8 bits work the same way with 256 stops.
This layout has three consequences. Half the patterns, those starting with 1, are negative, so the top bit doubles as a sign indicator. Zero appears once. And the range is slightly lopsided: 8 bits cover −128 to 127, because the negative half has one more stop than the positive half has after zero.
Negation
Counting backward is slow, so there is a shortcut for flipping the sign of any number: invert every bit, then add 1.
The reason follows from the loop. A number and its bitwise inverse always add up to all ones, and all ones is −1. So the inverse of 5 is −1 − 5 = −6. That is one step too far down, and the added 1 corrects it. Applying the procedure twice returns the original number.
Reading a Pattern Directly
To decode a pattern without counting, give the top bit a negative weight. In 8 bits the weights are −128, 64, 32, 16, 8, 4, 2, 1, and you add up the weights of the bits that are set.
This matches the loop. Read as ordinary unsigned binary, 11111011 is 251. The negative patterns are the unsigned values 128 to 255 shifted down by 256, and 251 − 256 = −5. Weighting the top bit as −128 instead of +128 subtracts exactly 256 whenever that bit is set, so both views give the same answer. For bits:
The same shift explains the name. The pattern for is the unsigned number , the complement of with respect to . Inverting gives , which is why the extra is needed.
Everything so far can be tried below. Flip bits, load a preset, or use the buttons. The formula lines show the weighted sum and the unsigned-minus- view side by side. Try min then Negate, or max then Add 1.
Why Hardware Uses It
An -bit adder works modulo and throws away the final carry. Since is stored as , adding it has the same effect as subtracting , with no sign checks:
00000111 (7)
+ 11111011 (-5)
----------
1 00000010 drop the carry out -> 2
Subtraction reuses the same circuit: , where the is supplied as the adder’s carry-in. A CPU needs no separate subtractor, and the same adder handles signed and unsigned operands. Only comparison, right shift, multiplication, and division need to know which interpretation is in use.
The older schemes lack this property:
| Scheme | −5 in 8 bits | Zeros | Adding signed values |
|---|---|---|---|
| Sign-magnitude | 10000101 | 2 | Raw addition fails; hardware must compare signs |
| Ones’ complement | 11111010 | 2 | Needs an end-around carry |
| Two’s complement | 11111011 | 1 | Same adder as unsigned |
In sign-magnitude, gives 00000101 + 10000011 = 10001000, which reads as −8. Sign-magnitude survives only in IEEE 754 floating point, which is why floats have both +0 and −0.
Gotchas
- The range is lopsided. In 8 bits, negating −128 gives
10000000again, because +128 does not fit. In C,abs(INT_MIN)overflows. - The same bits mean different numbers.
11111011is 251 unsigned and −5 signed. In C,-1 < 1uis false because −1 converts to the largest unsigned value. - Widening requires sign extension. Copy the sign bit into the new upper bits:
11111011becomes1111111111111011and stays −5. Zero-extending it would give 251. - Right shifts differ. A signed (arithmetic) right shift copies the sign bit, so
11111000(−8) shifted by 1 gives11111100(−4). An unsigned shift fills with zeros. - Overflow wraps silently in hardware.
01111111 + 00000001 = 10000000, so 127 + 1 = −128. Overflow occurred when two operands of the same sign produce a result of the opposite sign.
[!WARNING] In C and C++, signed integer overflow is undefined behavior even though the hardware wraps. Compilers may assume it never happens and optimize accordingly. Unsigned overflow is well defined and wraps modulo .
Related articles in Computer Architecture
- 2D vs 1D Memory: Why DLD and Assembly See Memory Differently — Why memory is a 2D grid in DLD but a 1D byte array in assembly, and how memory controllers connect the two views.
- GPU Compute, Latency, and Throughput — Understand GPU acceleration, memory constraints, inference latency, and throughput for AI workloads.
- How Computers Translate 1s and 0s into Screen Images — How binary memory states are mapped into RGB subpixel voltages and flashed onto physical monitor grids.