Data Representation & Number Systems

two's complement

Two's complement is the way essentially every modern computer stores signed integers, and it is worth understanding why it won so completely. The clever idea is to make the topmost bit carry a negative weight. In an n-bit number the top bit, instead of being worth +2^(n-1), is worth minus 2^(n-1), while all the other bits keep their normal positive place values. So in 8 bits the top bit is worth -128 and the rest are worth +64, +32, ..., +1. A negative number is simply one whose most significant bit is set.

There is also a quick recipe to negate: flip every bit and then add one. Take +5 in 8 bits, 0000 0101; flip to get 1111 1010; add one to get 1111 1011, which is -5. Check it with the place values: -128 + 64 + 32 + 16 + 8 + 2 + 1 = -5. The two killer advantages fall out of this design. First, there is exactly one zero: 0000 0000, and no separate negative zero, because flipping zero and adding one wraps neatly back to zero. Second, and the reason hardware designers cheered, addition and subtraction use the very same binary adder as unsigned numbers — to compute a minus b you just add a to the two's-complement negation of b, so one adder circuit serves signed and unsigned alike, with no special sign-handling logic.

The price is an asymmetric range. An n-bit two's complement number runs from minus 2^(n-1) up to 2^(n-1) minus 1 — for 8 bits that is -128 to +127. There is one more negative value than positive, because the single zero sits on the positive side. This creates a genuine sharp edge: the most negative number has no positive counterpart, so negating -128 in 8 bits overflows and gives back -128. Knowing this is real engineering, not trivia — that one missing positive has caused actual bugs, for instance taking the absolute value of the most-negative integer.

To compute 7 minus 3 in 8-bit two's complement: -3 is 1111 1101 (flip 0000 0011 to 1111 1100, add one). Then 0000 0111 + 1111 1101 = 0000 0100, which is 4 (the carry off the left is simply discarded). One adder did a subtraction.

Subtraction as addition of a negated operand — the same adder serves signed and unsigned.

The range is lopsided: n bits cover minus 2^(n-1) to 2^(n-1) minus 1, so the most negative value has no positive twin. Negating it overflows back to itself — a real bug source when taking absolute values.

Also called
twos complement2's complement補數表示法