Learning LabExplorable explanations
← All artifacts
Computer Systems

Two's Complement: Signed vs Unsigned

The same bits mean 200 unsigned and -56 signed. Toggle them, walk the number wheel where 127 wraps to -128, and negate a value by flipping every bit and adding one.

twos-complementbinarysigned-integersoverflow
LiveInteractive · drag, toggle, run it
Computer Systems / Number representation

Two's Complement: Signed vs Unsigned

A register holds nothing but bits. Whether a pattern means a small positive number or a large negative one depends entirely on how you decide to read it. Two's complement is the convention that lets the same adder serve both readings, gives zero a single encoding, and turns the top bit into a negative place value.

0b00000000
Unsigned
0
Signed
0
Unsigned
0=0
Every place is a positive power of two, the top one is +2⁷ = 128.
Signed (two's complement)
0=0
The top bit carries weight -2⁷ = -128. Every other place stays positive.
The number wheel

All 256 patterns laid out in a ring, counting up clockwise from 0 at the top. The pointer sits at the current pattern. Increment past the bottom and the unsigned reading wraps from 255 back to 0 while the signed reading wraps from 127 to -128. Both wraps happen at the same physical place: that is arithmetic modulo 28.

0255→ 0 / 127→-1280u 0s 0
unsigned wrap (255→ 0)signed wrap (most negative -128)current pattern
Negation: flip the bits, add one

To negate a two's complement number you invert every bit, then add one. This works because a number plus its bitwise complement is all ones, which is -1 in signed terms, so adding one lands on exactly -1 times the original. Watch it on the current value.

x00000000signed 0
~x11111111every bit inverted
~x + 100000000signed 0
Check: -(0) should be 0. Flip-and-add-one gives 0. They match.
Signed addition and overflow

The processor adds the bit patterns and keeps the low 8 bits. If the true signed sum falls outside [-128, 127] the result wraps and the sign flips the wrong way. That is signed overflow.

00101000
+
01000110
00101000+01000110=01101110
true signed sum 110, stored as 110 (unsigned 110)
No overflow. The true sum fits, so the stored signed value is correct.
Why two's complement wins

One zero, and addition that just works. Sign-magnitude and ones' complement both waste a pattern on negative zero. Two's complement has a single zero, and the same binary adder gives the right answer for signed and unsigned alike, because it is really addition modulo 28.

The sign bit is a negative place value. Reading signed, the top bit is not a flag bolted on the side, it contributes -2. Set it alone and you get the most negative number; the lower bits then only ever add back toward zero.

One more negative than positive. The range is [-128, 127], asymmetric by one. There is a pattern for -128 but none for 128, which is why negating the most negative value lands back on itself and is the classic overflow trap.

The bit math: masking to 8 bits, sign extension from the top bit, and addition modulo 28 = 256.