Computer Architecture • Number Representation • 7 min read

Two's Complement: How Computers Represent Negative Integers

Summary

Two's complement is the standard binary encoding for signed integers. Negative numbers are the bit patterns you reach by counting backward from zero and wrapping around. To negate a number, invert all bits and add 1. It is used because the same adder circuit handles signed and unsigned arithmetic, and zero has only one representation.

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.

All 3-bit two's complement values on one loop
Adding 1 always steps clockwise, including from −1 to 0. Blue is zero, green is positive, red is negative.

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.

Negating +5 in 8 bits
Inverting lands one short of the answer; adding 1 finishes the negation.

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.

Decoding the 8-bit pattern 11111011
The sign bit subtracts 128; the remaining set bits add 123, giving −5.

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 nn bits:

v=−bn−1 2n−1+∑i=0n−2bi 2iv = -b_{n-1}\,2^{n-1} + \sum_{i=0}^{n-2} b_i\,2^i

The same shift explains the name. The pattern for −x-x is the unsigned number 2n−x2^n - x, the complement of xx with respect to 2n2^n. Inverting gives 2n−1−x2^n - 1 - x, which is why the extra +1+1 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-2n2^n view side by side. Try min then Negate, or max then Add 1.

Width Load
Click any bit to flip it, or press Negate.
Signed value0
Unsigned value0
Signed range−128 to 127

Why Hardware Uses It

An nn-bit adder works modulo 2n2^n and throws away the final carry. Since −x-x is stored as 2n−x2^n - x, adding it has the same effect as subtracting xx, with no sign checks:

    00000111    (7)
  + 11111011    (-5)
  ----------
  1 00000010    drop the carry out -> 2

Subtraction reuses the same circuit: a−b=a+¬b+1a - b = a + \lnot b + 1, where the +1+1 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 bitsZerosAdding signed values
Sign-magnitude100001012Raw addition fails; hardware must compare signs
Ones’ complement111110102Needs an end-around carry
Two’s complement111110111Same adder as unsigned

In sign-magnitude, 5+(−3)5 + (-3) 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 10000000 again, because +128 does not fit. In C, abs(INT_MIN) overflows.
  • The same bits mean different numbers. 11111011 is 251 unsigned and −5 signed. In C, -1 < 1u is false because −1 converts to the largest unsigned value.
  • Widening requires sign extension. Copy the sign bit into the new upper bits: 11111011 becomes 1111111111111011 and 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 gives 11111100 (−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 2n2^n.

Related articles in Computer Architecture

Image preview

Fit