One's and two's complement: signed integers

Table of Contents

Binary arithmetic — how machines subtract by adding

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 A−B becomes A+(−B), 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 x with respect to a fixed total T is what x needs added to it to reach T: it is T−x. Choose T=2n in n 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 x with respect to T is the amount that must be added to x to reach exactly T, equivalently, T−x. 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 T=99 (all nines, one less than the modulus) and T=100 (the modulus itself, 102). They give the 9’s complement and the 10’s complement. The 9’s version has a gift: since 99−x subtracts each digit of x 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 TCT(x)=T−xx+CT(x)=TThe defining property is the second line: a number plus its complement makes the reference total, exactly.
Decimal, k digits10k complement (radix): C10k(x)=10k−x10k−1 complement (diminished radix): C10k−1(x)=(10k−1)−xThe diminished-radix version is digitwise: each digit d maps to 9−d, no borrows. And the two are one apart: radix complement = diminished complement + 1.
Binary, n bitsTwo’s complement: C2n(x)=2n−xOne’s complement: C2n−1(x)=(2n−1)−xWhy “flip every bit” computes the one’s complement, with no step skipped:x+x=111…1=2n−1x=(2n−1)−xBit by bit, x and its flip xhold opposite bits, so every column sums to 1 with no carries: the total is the all-ones pattern, whose value is 2n−1. Then the two’s complement is one step further:C2n(x)=x+1=(2n−1−x)+1=2n−x
Why the modulus total gives negationKeep all terms, then reduce. Working modulo 2n, which is exactly what an n-bit register does, since a carry out of the top bit falls off the end:−x≡−x+2n−2n≡(2n−x)−2n≡2n−x(mod2n)So inside a register, the pattern 2n−x behaves in every addition exactly as −x does. The two’s complement is not an encoding trick layered on top of arithmetic — it is negation, in the only arithmetic the hardware has.

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: x plus its radix complement reaches the modulus — and a fixed-width register keeps only the low digits, leaving 0.

Complement explorer controls

629’s complement (99 − x)
6310’s complement (100 − x)
11011010One’s complement (flip bits)
11011011Two’s complement (one’s + 1)

Complements turn subtraction into addition — already in decimal

  • Compute 52−37 with the 10’s complement: 52+63=115, drop the carried 1 (the modulus leaving the register), and the answer is 15.
  • With the 9’s complement: 52+62=114. Nothing can be dropped — instead the carried 1 is added back at the bottom: 14+1=15. That wrap-around has a name you will meet again in Step 6: the end-around carry.
  • 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 n-bit register holds 2n patterns. Unsigned arithmetic spends them all on 0 to 2n−1. To also write negative numbers, three systems compete — all three agree on the positives (sign bit 0, plain binary magnitude) and differ only in how a pattern with sign bit 1 is read.

Sign-magnitudeThe top bit is a pure sign flag; the remaining n−1 bits hold |x|. Reading is effortless — this is how humans write −5, but the adder must compare signs and magnitudes before it can even start, and 0 gets two patterns: 0000 and 1000 (+0 and −0).
One’s complementA negative is the bitwise flip of its positive: pattern value =−x~ read as “flip to negate”. Negation is free and symmetric, but 0 still has two patterns (0000 and 1111), and addition needs the end-around carry correction of Step 6.
Two’s complementA negative is the flip plus one: pattern p with top bit 1 has value p−2n. Equivalently, read the bits as a weighted sum where the top bit weighs −2n−1 instead of +2n−1. One pattern for zero, one plain unsigned adder for everything — and the spend is asymmetric: one extra negative, −2n−1, with no positive partner.

Figure 2 — The value −5 in 4 bits, three ways

sign bitmagnitude / value bits

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.

Encoding explorer controls

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.

ValueSign-magnitudeOne’s complementTwo’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 2n−1 to the full modulus 2n.

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.

Negation lab controls

00011001Pattern of x
11100111Pattern after negation
−25Reads as

Edge cases, one per system

  • Sign-magnitude and one’s complement: negating 0 produces the other zero: 10000000 or 11111111. Harmless, but code that compares patterns bit-for-bit sees two different zeros.
  • Two’s complement: negating −128 (pattern 10000000) flips to 01111111 and adds 1, landing back on 10000000, itself. There is no +128 in 8 bits to land on. In C this is why 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 2n, adding two patterns bit by bit — carries and all, with any carry out of the top bit simply discarded — produces the pattern of the true sum, whenever that sum fits the range. No sign comparison, no magnitude subtraction, no cases: the same circuit that adds unsigned numbers adds signed ones, and the result is even correct read as unsigned when both inputs are. (One’s complement almost manages this: its additions are correct up to the end-around carry repair of Step 6. Sign-magnitude addition, by contrast, is a small decision tree of its own.)

Figure 4: 25 + (−9) = 16, added as raw patterns

sign bit columncarry into a bitsum bits

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.

Adder controls

16True sum A + B
16Pattern read as two’s complement
16Patterns added as unsigned
fitsSigned range check

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 identityA−B=A−B+2n−2n=A+(2n−B)−2n≡A+(2n−B)(mod2n)Adding and subtracting 2n changes nothing; regrouping exposes 2n−B, the two’s complement of B; and the trailing −2n is precisely the carry-out the register discards. In one’s complement the same identity holds with 2n−1 in place of 2n, and the discarded amount comes back as the end-around carry, which is why that system needs Step 6.

Figure 5: 25 − 9 in two’s complement: negate 9, then add

How to read this. Row by row: A, then B, then −B built by the chosen system’s negation rule, then the raw addition, then the finished result after the system’s carry rule (discard for two’s, wrap around for one’s). The value at the right of each row is that pattern read in the chosen system.

Subtraction controls

16True difference A − B
16Machine result
1Carry out of the addition

Reading the carry-out in a subtraction

  • Two’s complement: the carry-out is pure exhaust — the 2n from the identity leaving the register. Discard it unconditionally. (Some architectures keep it as a “no borrow” flag, which is bookkeeping, not arithmetic.)
  • One’s complement: a carry-out of 1 means the raw sum is one short — the missing 2n−1 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.
  • Try A=9, B=25 in both systems: two’s complement shows carry-out 0 and pattern 11110000 (−16 directly); one’s shows carry-out 0 and 11101111, 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 2n−1, not 2n, but the register discards multiples of 2n. Each carry out of the top bit therefore throws away 2n when it should only have thrown away 2n−1: the sum comes out exactly 1 too small. The fix is to take that carry and add it back in at the least significant bit — the carry ends around. Two’s complement, whose modulus is the register’s own 2n, discards exactly the right amount and needs no repair. Step through all four behaviours:

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.

Stepper controls

Step 1 of 4

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 [−2n−1,2n−1−1]. Step outside it and the pattern still forms — addition never fails — but it now encodes a value of the wrong sign: 100+50 in 8 bits produces 10010110, which reads as −106. That event is signed overflow, and it is not the carry-out: 100+(−50) also produces a carry-out of 1, yet its answer 50 is exactly right. Overflow has a precise test, in two equivalent forms:

Sign ruleOverflow 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 A−B as A+(−B): overflow needs A and B of opposite signs and a result whose sign differs from A’s.
Carry rule (what hardware tests)Let cin be the carry into the sign bit and cout the carry out of it. ThenV=cin⊕coutOverflow (V=1) exactly when the two carries differ — the sign column’s arithmetic disagrees with its neighbours about which side of the boundary the sum fell on. This XOR is the overflow flag in every mainstream CPU.

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 explorer controls

1Carry into sign bit
0Carry out of sign bit
150True sum
−106Pattern reads as

Overflow vs carry-out — keep them separate

  • Carry-out = 1 means the unsigned sum exceeded 2n−1. It fires constantly in perfectly correct signed arithmetic — every time a negative addend is involved, roughly.
  • 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, 2n patterns — and the range table is just the accounting. Sign-magnitude and one’s complement split the budget evenly between negative and positive but waste one pattern on a second zero, so both stop at ±(2n−1−1). Two’s complement spends the would-be second zero on one more negative value, reaching −2n−1: the range is asymmetric by exactly one, the same asymmetry that made −128 un-negatable in Step 3. Drag the width and watch the bars rebalance — widths are drawn on a log scale so that 4 bits and 16 bits can share one picture; the readouts carry the exact endpoints.

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 log2 of the count. The chips at the centre count the zero patterns: two for sign-magnitude and one’s complement, one for two’s. Unsigned is shown for scale — it spends everything on non-negatives.

Range controls

0 … 255Unsigned range
−127 … 127Sign-magnitude / one’s (2 zeros)
−128 … 127Two’s complement (1 zero)
256Total patterns 2^n

Width nUnsignedSign-magnitudeOne’s complementTwo’s complement

9 — The three systems, side by side

PropertySign-magnitudeOne’s complementTwo’s complement
Negative of xFlip the sign bitFlip every bitFlip every bit, add 1
Patterns for zero2 (0000, 1000)2 (0000, 1111)1 (0000)
Range, 8 bits−127 … 127−127 … 127−128 … 127
AdditionCompare signs; add or subtract magnitudesPlain add, then end-around carryPlain add, discard carry-out
SubtractionDecision tree on signsFlip subtrahend, add, end-around carryFlip subtrahend, add 1, add
Quirk to rememberReading is trivial; arithmetic is not−0 exists; one repair step per addition−128 has no positive partner; negation can overflow
Where it lives todayIEEE 754 sign bitInternet checksum (IPv4 / TCP / UDP)Every general-purpose integer unit

The arc, in one breath. A complement is always relative: the complement of x with respect to a total T is T−x. With respect to 2n−1 that partner is a mere bit-flip away but leaves arithmetic one short, repaid by the end-around carry; with respect to 2n it is exactly negation in register arithmetic, so a single adder adds, subtracts and negates — and the only remaining hazards are the finite range: overflow when a sum’s sign flips, and the lone extra negative at the bottom that has nothing to flip to.

One’s and Two’s Complement — twelfth in the visual explainer series. Every pattern, carry and verdict on this page is computed live by the figures’ own bit-level adder, using the definitions in Step 1.