One's and two's complement: signed integers
A computer’s adder only knows how to add. Subtraction, negative numbers, and the sign bit itself all have to be built out of that one operation — and the trick that makes it possible is the complement: for every number, a partner that completes it to a round total. Flip every bit and you get the one’s complement; flip and add one and you get the two’s complement, and with it becomes , an addition. This page builds the idea from the definition up: what a complement is with respect to a number, the three historical ways to write signed values, negation, addition, subtraction by addition, the end-around carry that one’s complement arithmetic needs, signed overflow, and the exact range each system buys you.
The one-sentence idea. The complement of with respect to a fixed total is what needs added to it to reach : it is . Choose in bits and that partner is the negation — so subtraction collapses into addition, and one adder circuit does everything.
1 — What a complement is, with respect to a number
“Complement” is a relative word: a number never has a complement in the abstract, only a complement with respect to some fixed reference total. The complement of with respect to is the amount that must be added to to reach exactly , equivalently, . Two gears meshing, two angles making a right angle, two quantities making a round sum: in every case the complement is defined by the total it completes.
The reference totals that matter are the roundest ones in each base. In two-digit decimal the candidates are (all nines, one less than the modulus) and (the modulus itself, ). They give the 9’s complement and the 10’s complement. The 9’s version has a gift: since subtracts each digit of from 9 independently, it needs no borrows — each digit is simply flipped to its partner under 9 (0↔9, 1↔8, 2↔7, 3↔6, 4↔5). In binary the same two choices reappear, and the digit-flip gift becomes a bit-flip.
| Complement with respect to | The defining property is the second line: a number plus its complement makes the reference total, exactly. |
|---|---|
| Decimal, digits | The diminished-radix version is digitwise: each digit maps to , no borrows. And the two are one apart: radix complement = diminished complement + 1. |
| Binary, bits | Why “flip every bit” computes the one’s complement, with no step skipped: |
| Why the modulus total gives negation | Keep all terms, then reduce. Working modulo |
Figure 1 — The complements of 37
How to read this. Left: two-digit decimal, reference totals 99 and 100. Right: the same value in 8 bits, reference totals 255 (all ones) and 256. In both bases the diminished complement is a digitwise/bitwise flip needing no borrows, and the radix complement is exactly one more. Watch the sums:
Complements turn subtraction into addition — already in decimal
- Compute
with the 10’s complement:52 − 37 , drop the carried 1 (the modulus leaving the register), and the answer is 15.52 + 63 = 115 - With the 9’s complement:
. Nothing can be dropped — instead the carried 1 is added back at the bottom:52 + 62 = 114 . That wrap-around has a name you will meet again in Step 6: the end-around carry.14 + 1 = 15 - Mechanical calculators and early computers genuinely subtracted this way: one adder, no subtractor, no borrow chains.
2 — Sign-magnitude vs one’s vs two’s complement
An
| Sign-magnitude | The top bit is a pure sign flag; the remaining 0000 and 1000 ( |
|---|---|
| One’s complement | A negative is the bitwise flip of its positive: pattern value 0000 and 1111), and addition needs the end-around carry correction of Step 6. |
| Two’s complement | A negative is the flip plus one: pattern |
Figure 2 — The value −5 in 4 bits, three ways
How to read this. All three rows encode the slider’s value in 4 bits. Positives are identical in every system; the systems only diverge below zero. Slide to 0 and note the second zero pattern each of the first two systems keeps in reserve; slide to −8 and only two’s complement still has a pattern.
The full 4-bit map. Every pattern is used by every system — the systems are three different readings of the same sixteen patterns, which is why the same hardware adder can serve all of them.
| Value | Sign-magnitude | One’s complement | Two’s complement |
|---|
Why two’s complement won. Sign-magnitude survives where humans read the bits (the sign bit of IEEE floating point) and one’s complement survives inside the Internet checksum (Step 6), but general-purpose integer arithmetic is two’s complement everywhere: a single zero, positives and negatives through one adder, and subtraction with no special cases. The price — one un-negatable value at the bottom of the range — is Step 3’s edge case and Step 8’s asymmetry.
3 — Negation: each system flips something different
Negation is where the three systems show their personalities most plainly. Sign-magnitude flips one bit — the sign — and touches nothing else. One’s complement flips every bit: negation is the complement operation, which is exactly the diminished-radix complement of Step 1. Two’s complement flips every bit and adds one, completing the move from the diminished total
Figure 3 — Negating 25 in two’s complement: flip, then add 1
How to read this. The left panel is the value’s own pattern in the chosen system; the panels to its right apply that system’s negation rule, one mechanical step at a time. Bits the rule flips are tinted teal; a bit the rule leaves alone keeps the plain surface.
Edge cases, one per system
- Sign-magnitude and one’s complement: negating 0 produces the other zero:
10000000or11111111. Harmless, but code that compares patterns bit-for-bit sees two different zeros. - Two’s complement: negating
(pattern− 128 10000000) flips to01111111and adds 1, landing back on10000000, itself. There is no in 8 bits to land on. In C this is why+ 128 abs(INT_MIN)stays negative; the operation silently overflows (Step 7). - Try it above: set the two’s complement slider to −128 and watch the output pattern equal the input.
4 — Addition: one unsigned adder serves every sign
Here is the payoff of the two’s complement definition. Because a negative pattern is its value modulo
Figure 4: 25 + (−9) = 16, added as raw patterns
How to read this. Each column adds the two operand bits plus the carry arriving from the right; the digit above a column is that incoming carry. The leftmost column is the sign bit — it is added exactly like every other column, which is the whole point. A carry out of the top falls off the register and is ignored.
One adder, two interpretations. The hardware does not know whether you meant signed or unsigned — it produces one pattern and one carry-out flag. Software decides afterwards what the pattern means. The carry-out is the unsigned overflow signal; the signed overflow signal is subtler, and it is Step 7’s subject.
5 — Subtraction by addition: negate, then add
With negation reduced to a complement, subtraction needs no new hardware. The identity, with every term kept until the modulus absorbs it:
| The subtraction identity |
|---|
Figure 5: 25 − 9 in two’s complement: negate 9, then add
How to read this. Row by row:
Reading the carry-out in a subtraction
- Two’s complement: the carry-out is pure exhaust — the
from the identity leaving the register. Discard it unconditionally. (Some architectures keep it as a “no borrow” flag, which is bookkeeping, not arithmetic.)2 n - One’s complement: a carry-out of 1 means the raw sum is one short — the missing
bookkeeping — and it must be added back at the least significant bit. A carry-out of 0 means the raw pattern is already a finished (negative) one’s complement result: flip it to read its magnitude.2 n − 1 - Try
,A = 9 in both systems: two’s complement shows carry-out 0 and patternB = 25 11110000(−16 directly); one’s shows carry-out 0 and11101111, which flips to magnitude 16.
6 — End-around carry: the carry that comes back
Why does one’s complement addition need a repair that two’s complement addition doesn’t? The diminished modulus is the reason. Adding two one’s complement patterns works modulo
Figure 6 — End-around carry: 5 + (−3) in 4-bit one’s complement
How to read this. Patterns are 4-bit one’s complement. The gold box is the carry out of the top bit; the curved arrow is its path back to the least significant bit. When there is no carry out, there is nothing to wrap — and the raw pattern, sign bit set, is already the finished negative answer.
Where end-around carry still earns its keep. The Internet checksum (used by IPv4, TCP and UDP) sums a message in 16-bit words using one’s complement arithmetic — end-around carry included — and stores the complement of the sum. Why keep the older system? Endianness independence: the checksum comes out byte-swapped-identical on big- and little-endian machines, and a correctly checksummed message sums to all ones, which any receiver can verify with the same adder. Two’s complement arithmetic could not offer either property as cheaply.
7 — Overflow: the sign bit lies
Discarding the carry-out is safe only while the true result stays inside 10010110, which reads as
| Sign rule | Overflow happens exactly when the two addends have the same sign and the result’s sign differs from theirs. Adding numbers of opposite signs can never overflow: the true sum lies between them, hence inside the range. The same rule covers subtraction, reading |
|---|---|
| Carry rule (what hardware tests) | Let |
Figure 7: 100 + 50: overflow, the result reads −106
How to read this. The orange frame isolates the sign-bit column. Below it, the two carries that decide overflow: the carry arriving into the column from bit 6 and the carry leaving it. Equal carries — no overflow, whatever the carry-out alone says. Different carries — the pattern’s sign is a lie.
Overflow vs carry-out — keep them separate
- Carry-out = 1 means the unsigned sum exceeded
. It fires constantly in perfectly correct signed arithmetic — every time a negative addend is involved, roughly.2 n − 1 - Overflow = 1 means the signed sum left the representable range and the stored pattern has the wrong sign. This is the one signed code must check (or rule out by widening).
- Neither flag stops the addition. The pattern is always produced; the flags are the only record that something needs attention.
8 — Range: counting the patterns
Every system spends the same budget,
Figure 8 — The range of 8-bit signed systems
How to read this. Each bar is centred on zero; the teal length to the left counts the negative values, the blue length to the right the positives, drawn proportional to
| Width | Unsigned | Sign-magnitude | One’s complement | Two’s complement |
|---|
9 — The three systems, side by side
| Property | Sign-magnitude | One’s complement | Two’s complement |
|---|---|---|---|
| Negative of | Flip the sign bit | Flip every bit | Flip every bit, add 1 |
| Patterns for zero | 2 (0000, 1000) | 2 (0000, 1111) | 1 (0000) |
| Range, 8 bits | −127 … 127 | −127 … 127 | −128 … 127 |
| Addition | Compare signs; add or subtract magnitudes | Plain add, then end-around carry | Plain add, discard carry-out |
| Subtraction | Decision tree on signs | Flip subtrahend, add, end-around carry | Flip subtrahend, add 1, add |
| Quirk to remember | Reading is trivial; arithmetic is not | −0 exists; one repair step per addition | −128 has no positive partner; negation can overflow |
| Where it lives today | IEEE 754 sign bit | Internet checksum (IPv4 / TCP / UDP) | Every general-purpose integer unit |
The arc, in one breath. A complement is always relative: the complement of
