Skip to content
BetterDL

Two's complement

Also called: 2's complement, two's-complement representation, twos complement representation

The standard way to store signed integers: ordinary binary except the MSB has weight −2ⁿ⁻¹. n bits cover −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1.

Two's complement is how virtually every processor stores signed whole numbers. Read it like ordinary binary, with one change: the MSB's weight is negative.

In 4 bits the weights are −8, 4, 2, 1. So 0101 = 5, and 1011 = −8 + 2 + 1 = −5. In n bits the MSB is worth −2ⁿ⁻¹.

The intuition is a counter running backwards. Wind a 4-bit counter back one step from 0000 and every bit rolls over to 1111: that's −1. Another step gives 1110 = −2, and so on down to 1000 = −8. Every pattern with MSB 1 equals its unsigned value minus 2ⁿ.

Why hardware uses it:

  • One zero. Unlike sign magnitude, there's no −0.
  • One adder for everything. Adding the bits as if they were unsigned gives the right signed answer too, as long as it fits.
  • Easy negation. Invert every bit and add 1 (twos complement negation).
  • Subtraction is addition. A − B = A + (−B), so an adder subtractor needs only XOR gates and a carry-in.

The range is −2ⁿ⁻¹ to 2ⁿ⁻¹ − 1: one more negative value than positive. In 8 bits that's −128 to 127. Results outside it cause signed overflow, and widening a value needs sign extension.

1
−8
0
4
1
2
1
1

Worked examples

Example

Decode 8-bit 11111010

Two ways to read a negative pattern.

1
−128
1
64
1
32
1
16
1
8
0
4
1
2
0
1
  1. 1.

    Weights: −128 for the MSB, then 64, 32, 16, 8, 4, 2, 1.

  2. 2.

    1-bits: −128 + 64 + 32 + 16 + 8 + 2 = −6.

  3. 3.

    Shortcut: the unsigned value is 250, and 250 − 256 = −6.

Example

Encode −44 in 8 bits

Write +44, then negate it.

  1. 1.

    +44 = 32 + 8 + 4 = 00101100.

  2. 2.

    Invert every bit: 11010011.

  3. 3.

    Add 1: 11010100.

  4. 4.

    Check: −128 + 64 + 16 + 4 = −44.

Common mistakes

  • Reading a negative pattern as a sign plus a size: 4-bit 1011 is −5, not −3.

  • Inverting before padding to the full width, which flips too few bits.

  • Expecting +2ⁿ⁻¹ to fit. In 8 bits, +128 doesn't fit but −128 does.

  • Treating any carry-out as overflow. For two's complement, overflow depends on the signs, not on the carry-out.

Practice Two's complement

Interactive questions with instant feedback and a worked solution for every wrong answer.

Learn it step by step

Two's complement is taught in Number Systems and Adders and ALUs.