Negative numbers are stored in two's complement because it lets the same binary adder hardware handle both signed and unsigned arithmetic, eliminates the duplicate zero problem of sign-magnitude and ones' complement, and makes subtraction a simple addition of a negated value. In short, two's complement is the encoding that makes binary arithmetic work with the fewest special cases and the least extra circuitry.
The Problem: Representing Negative Values in Binary
Binary digits naturally represent only non-negative integers: each bit position is a power of two, and the sum of the set bits gives the value. To store negative numbers, a convention is needed to map some bit patterns to negative values. The earliest and most intuitive approach is sign-magnitude, where the most significant bit (MSB) is a sign flag and the remaining bits hold the magnitude. For an 8-bit byte, 00000101 is +5 and 10000101 is −5. This mirrors how humans write signed numbers, but it creates two zeros: 00000000 (+0) and 10000000 (−0). It also complicates arithmetic: adding +5 and −5 requires comparing signs and magnitudes rather than simply adding bits.
A second early scheme, ones' complement, represents −x as the bitwise NOT of x. For 8 bits, +5 is 00000101 and −5 is 11111010. Ones' complement still has two zeros (00000000 and 11111111) and requires an end-around carry during addition, where a carry out of the MSB is added back into the least significant bit. That extra step slows hardware and adds complexity. Two's complement solves both issues by defining −x as the bitwise NOT of x plus one, which produces a single zero and lets ordinary binary addition work without correction.
How Two's Complement Works
In an n-bit two's complement system, the MSB carries a weight of −2^(n−1) instead of +2^(n−1). For 8 bits, the place values from left to right are −128, 64, 32, 16, 8, 4, 2, and 1. The bit pattern 11111111 therefore equals −128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = −1. The pattern 10000000 equals −128, and 01111111 equals +127. This weighting gives a range of −128 to +127 for 8 bits, −32,768 to +32,767 for 16 bits, and −2,147,483,648 to +2,147,483,647 for 32 bits. The asymmetry — one more negative value than positive — is a direct consequence of having only one zero.
To negate a number in two's complement, invert all bits and add 1. For example, +5 in 8 bits is 00000101. Inverting gives 11111010; adding 1 gives 11111011, which is −5. Applying the same operation to −5 returns +5, confirming the symmetry. The single zero is 00000000; its negation is also 00000000 because inverting gives 11111111 and adding 1 produces a carry out that is discarded, leaving 00000000.
Why Addition and Subtraction Need No Special Cases
The central advantage of two's complement is that addition and subtraction use the same hardware. To compute 7 − 5, the processor adds 7 and the two's complement of 5. In 8 bits: 00000111 + 11111011 = 1 00000010. The carry out of the MSB is ignored, leaving 00000010, which is 2. No sign comparison, no end-around carry, no separate subtraction circuit. The same adder that sums unsigned bytes sums signed integers correctly, provided the result stays within range. This property is why essentially every general-purpose CPU uses two's complement for integer arithmetic.
Multiplication and division are more complex, but they too benefit from a single zero and a well-defined range. Hardware multipliers often convert operands to sign-magnitude internally, but the storage and addition paths remain two's complement. The encoding also makes bitwise operations predictable: shifting a two's complement number right by one bit is equivalent to integer division by two rounding toward negative infinity, which is why some languages define right shift that way for signed types.
Range, Overflow, and the Asymmetric Limit
Because the range is asymmetric, the most negative number has no positive counterpart. In 8 bits, −128 cannot be negated: inverting 10000000 gives 01111111, and adding 1 gives 10000000 again. This is not a bug but a mathematical consequence of the range. Arithmetic overflow occurs when a result falls outside the representable range. For signed addition, overflow is detected when the carry into the MSB differs from the carry out of the MSB. For example, adding +127 (01111111) and +1 (00000001) gives 10000000, which is −128 — a wrong signed result. Processors set an overflow flag in this case, and programming languages differ in whether they trap, wrap, or leave the behavior undefined.
Unsigned overflow is simpler: it is just the carry out of the MSB. The same bit pattern can be interpreted as signed or unsigned, which is why languages like C and C++ have both signed and unsigned integer types. The bits are identical; only the interpretation changes. This duality is a source of subtle bugs when signed and unsigned values are mixed in comparisons or arithmetic.
Comparison With Other Signed Representations
- Sign-magnitude: Intuitive for humans, but has two zeros, requires separate sign handling in arithmetic, and is rarely used for integer storage today. It survives in floating-point formats, where the sign is a separate bit.
- Ones' complement: Also has two zeros and needs end-around carry. It was used in some early computers but is now obsolete for integers.
- Two's complement: Single zero, no end-around carry, same adder for signed and unsigned. It dominates modern computing.
- Offset binary (excess-K): Stores a value plus a constant so that all bit patterns represent non-negative numbers. Used in some analog-to-digital converters and floating-point exponents, but not for general integer arithmetic.
The choice is not merely historical. Two's complement minimizes the number of distinct cases in hardware design, which reduces transistor count and power consumption. In an era when adders were built from discrete logic, this mattered enormously; today it still simplifies CPU design and verification.
Common Misconceptions and Practical Pitfalls
A frequent misconception is that the MSB is a simple sign bit. In two's complement it is a sign bit in the sense that 1 indicates a negative number, but it also carries weight −2^(n−1). Flipping it does not negate the number; only the full invert-and-add-one operation does. Another misconception is that two's complement is only for integers. Floating-point formats use sign-magnitude for the sign, but their exponents are often stored in a biased (offset) form, not two's complement. The reason is that floating-point comparison is easier when the exponent field is ordered like an unsigned integer.
In programming, mixing signed and unsigned types can produce surprising results. In C, comparing a signed negative number with an unsigned number converts the signed value to unsigned, making a negative number appear very large. This is a common source of bugs in loops and boundary checks. Languages like Java and C# avoid unsigned integers for general use, partly to sidestep these issues. When working with fixed-width integers, always check the range and use the appropriate type; for example, a 16-bit signed integer cannot hold 40,000, but a 16-bit unsigned integer can.
Frequently Asked Questions
Why does two's complement have one more negative number than positive?
Because zero occupies one of the patterns that would otherwise be a positive number. With 8 bits there are 256 patterns; one is zero, leaving 255 for positive and negative values. The negative side gets 128 values (−128 to −1) and the positive side gets 127 (1 to 127).
Can two's complement represent −0?
No. The bit pattern that would be −0 in sign-magnitude (10000000) represents −128 in 8-bit two's complement. There is only one zero, 00000000.
How do I convert a negative number to two's complement?
Write the positive value in binary using the full width, invert every bit, then add 1. For example, to represent −3 in 8 bits: +3 is 00000011, invert to 11111100, add 1 to get 11111101.
Is two's complement used in all computers?
Nearly all general-purpose CPUs today use two's complement for signed integers. Some specialized processors or historical machines used other representations, but they are rare. The dominance is due to hardware simplicity and the single-zero property.
What happens if I negate the most negative number?
The result is the same number, because its positive counterpart is not representable. In 8 bits, negating −128 gives −128. This is an overflow condition that some languages trap and others allow to wrap.
Why not use sign-magnitude for integers?
Sign-magnitude requires separate handling of signs in addition and subtraction, has two zeros, and complicates comparison. Two's complement lets the same adder handle both signed and unsigned values, which is simpler and faster.