How Two's Complement Works
Two's complement is the standard way computers represent signed (positive and negative) integers in binary. Instead of reserving a bit purely as a "+/-" flag (sign-magnitude) or inverting bits alone (one's complement), two's complement encodes a negative number x, within an n-bit width, as 2n + x — its value modulo 2n. This single trick gives binary integers exactly one representation of zero and lets a processor add and subtract signed numbers with the same hardware it uses for unsigned numbers.
Formula and method
For a non-negative decimal value, the n-bit two's complement representation is just the ordinary binary form of the number, padded with leading zeros to n bits. For a negative value x (where -2n-1 ≤ x ≤ -1), compute the unsigned n-bit pattern as 2n + x, then convert that unsigned result to binary. In practice this is usually done with the "invert and add one" shortcut: write the binary form of |x|, flip every bit (0↔1) to get the one's complement, then add 1. Both methods produce the same bit pattern. To reverse the process — decoding an n-bit two's complement string back to decimal — read the bits as an unsigned binary number; if the leading (most significant) bit is 0, that unsigned value is the answer, and if it is 1, subtract 2n from the unsigned value to recover the negative result.
Worked example
Encode -76 in 8 bits: |−76| = 76 = 01001100 in binary. Invert every bit to get the one's complement, 10110011, then add 1 to get 10110100 — the two's complement of -76. Check it against the direct formula: 28 + (-76) = 256 - 76 = 180, and 180 in binary is indeed 10110100. Decoding it back: the leading bit is 1, so the value is negative; the unsigned value of 10110100 is 180, and 180 - 256 = -76, matching the original input.
Common sources of error
- Wrong bit width: the same bit pattern means different decimal values at different widths — 10110100 is -76 as an 8-bit number but a large positive number if treated as 16 or 32 bits without sign-extension.
- Off-by-one on the range: an n-bit two's complement number covers -2n-1 to 2n-1 - 1, not -2n-1 to 2n-1 — there is one more negative value than positive value because zero only uses one code.
- Confusing one's complement with two's complement: one's complement is just the bitwise NOT; two's complement is one's complement plus 1, and only two's complement has a single representation of zero.
- Overflow: adding two n-bit two's complement numbers whose true sum falls outside the representable range wraps around silently — always check the operands against the signed range first.
Applications
Two's complement is used almost universally in digital hardware and programming languages (C, Java, Python's fixed-width integer types, CPU registers) to store signed integers, because addition, subtraction, and multiplication of two's complement numbers use the exact same binary adder/multiplier circuits as unsigned arithmetic — the hardware does not need a separate "subtract" circuit or special-case logic for the sign. It also shows up when reading raw binary or hexadecimal dumps (network packets, file formats, machine code) where a signed field must be decoded correctly, and in embedded systems and low-level debugging where engineers convert between hex, binary, and signed decimal by hand.