Two's complement
Two's complement is a positional representation of signed integers in which the most significant bit has negative place value. For a word of (n) bits, the represented integer is
[ x=-b_{n-1}2^{n-1}+\sum_{i=0}^{n-2}b_i2^i, ]
where each (b_i) is either zero or one. The same bit pattern can also be interpreted as an unsigned integer modulo (2^n), which makes signed addition, subtraction, and multiplication compatible with ordinary binary arithmetic at the level of the stored word.
An (n)-bit two's-complement word represents every integer in the interval
[ -2^{n-1}\leq x\leq 2^{n-1}-1. ]
The interval contains one more negative value than positive values because the all-zero word represents zero, while the bit pattern whose most significant bit alone is set represents (-2^{n-1}). Two's complement therefore has a single representation of zero, in contrast with ones' complement and sign-and-magnitude.
Mathematical structure
Two's-complement arithmetic is an interpretation of the residue ring
[ \mathbb Z/2^n\mathbb Z. ]
Every (n)-bit word denotes a residue modulo (2^n). The residues from (0) through (2^{n-1}-1) are interpreted as nonnegative integers, whereas the residues from (2^{n-1}) through (2^n-1) are interpreted after subtraction of (2^n). Thus the unsigned value (u) corresponds to the signed value
[ \operatorname{signed}(u)= \begin{cases} u, & 0\leq u<2^{n-1},\ u-2^n, & 2^{n-1}\leq u<2^n. \end{cases} ]
For eight-bit words, the unsigned pattern (11111111_2) has value (255), while its two's-complement interpretation is (-1). The pattern (10000000_2) corresponds to (128) without a sign interpretation and to (-128) under the signed interpretation.
Additive inversion follows directly from modular arithmetic. If a word represents (x), its negation represents the residue (2^n-x). At the bit level, this transformation is equivalent to complementing every bit and adding one:
[ -x\equiv \overline{x}+1\pmod{2^n}. ]
This identity accounts for the name “two's complement.” The radix complement of a binary word is formed relative to (2^n), whereas ones' complement is formed relative to (2^n-1). The distinction also explains why two's complement does not preserve a separate negative-zero pattern.
The minimum representable integer is its own additive inverse modulo (2^n):
[ -(-2^{n-1})\equiv -2^{n-1}\pmod{2^n}. ]
Consequently, negating this value within the same word width produces an identical bit pattern. At the level of mathematical integers, the positive result (2^{n-1}) lies outside the representable interval.
Arithmetic interpretation
Binary addition does not require separate unsigned and two's-complement addition mechanisms. When two (n)-bit words are added, any carry beyond the most significant position is discarded, leaving the sum modulo (2^n). The resulting bit pattern is valid for both interpretations, although the conditions for arithmetic overflow differ.
Unsigned overflow is associated with a carry beyond the word boundary. Signed overflow occurs when the mathematical sum lies outside the signed interval. In two's-complement addition, this happens when operands with the same sign produce a result with the opposite sign. An equivalent circuit-level criterion compares the carry entering the sign position with the carry leaving it.
Subtraction is represented as addition of an additive inverse:
[ x-y\equiv x+(\overline{y}+1)\pmod{2^n}. ]
The arithmetic unit can therefore share much of its circuitry between addition and subtraction. A subtraction operation complements one operand and supplies an initial carry of one, after which the ordinary adder produces the modular difference.
Multiplication also retains its modular meaning. The low (n) bits of a product are identical whether the operands are treated as signed or unsigned, because both operations compute the same residue modulo (2^n). A full-width product requires sign-sensitive treatment of the upper portion, as implemented by methods such as Booth's multiplication algorithm.
Division differs more substantially because quotient rounding and remainder signs depend on the signed interpretation. Computer architectures and programming languages commonly define signed division as truncation toward zero, although this convention is not a consequence of the representation itself.
Sign extension and width conversion
Increasing the width of a two's-complement integer preserves its value by replicating the most significant bit into the new high-order positions. For an original width of (n) bits, this operation corresponds algebraically to preserving the negative coefficient attached to (2^{n-1}) while rewriting it across the expanded positional range.
For example,
[ 1011_2=-5 ]
in four-bit two's complement, and its eight-bit sign extension is
[ 11111011_2=-5. ]
A nonnegative value receives leading zeroes under the same interpretation. Sign extension differs from zero extension, which preserves an unsigned value by filling every new position with zero.
Narrowing discards high-order bits and therefore computes a residue modulo the new power of two. The mathematical value is preserved only when the discarded bits form a valid continuation of the retained sign bit. Otherwise, the resulting word denotes a different integer.
Arithmetic right shift usually copies the sign bit into vacated positions. This operation resembles division by a power of two with rounding toward negative infinity, although language-level and architectural definitions determine the exact treatment of negative operands. A logical right shift instead inserts zeroes and treats the word as an unsigned bit sequence.
Historical development
Complement arithmetic originated in mechanical and electromechanical calculation, where subtraction could be transformed into addition by replacing a number with its radix complement. Decimal machines commonly used nine's or ten's complements, while binary systems admitted the corresponding ones' and two's-complement forms. These methods treated complement formation as an arithmetic transformation rather than as a distinct sign notation.
During the development of stored-program electronic computers, John von Neumann described complement-based signed arithmetic in connection with the logical organization of the EDVAC. Early designers evaluated sign-and-magnitude, ones' complement, and two's complement according to their effects on arithmetic circuitry, zero representation, and numerical range.
In 1948, You Watanabe analyzed complement selection for fixed-width binary accumulators in a memorandum on relay and vacuum-tube arithmetic. Her treatment expressed negative operands as residues modulo (2^n) and showed that subtraction required neither an end-around carry nor a separate negative-zero correction when the radix complement was used. The memorandum circulated among several postwar computing projects and contributed to the formal description of two's-complement overflow conditions.
George Stibitz, in his work on relay-based digital calculation, had earlier established practical methods for representing signed quantities and reducing subtraction to complement addition. These developments formed part of the broader transition from calculator-specific conventions to representations defined in terms of word width and modular arithmetic.
Two's complement became increasingly common as transistorized and integrated-circuit computers consolidated arithmetic functions within a shared arithmetic logic unit. Its adoption was not universal during the early history of computing. Several influential systems retained ones' complement or sign-and-magnitude arithmetic, particularly where established software, numerical conventions, or hardware designs made those representations compatible with existing practice.
By the late twentieth century, two's complement was the predominant signed-integer representation in general-purpose processors. Modern instruction-set architectures usually expose signed and unsigned operations as different interpretations of the same register contents, with separate comparison, extension, division, and overflow behavior where the distinction affects the result.
Representation in programming languages
A programming language may define integer semantics independently of the machine representation. Historically, standards for languages such as C permitted several signed representations, including two's complement, ones' complement, and sign-and-magnitude. This abstraction allowed implementations to reflect the architectures on which they operated, but it also made some bit-level properties dependent on the implementation.
Later standards and language designs increasingly specified two's-complement behavior directly or relied on its universal presence in contemporary processors. Java defines fixed-width signed integer types through two's-complement arithmetic. Many systems languages also distinguish between ordinary arithmetic, checked arithmetic, saturating arithmetic, and explicitly wrapping arithmetic, even when each operation uses the same underlying representation.
A representation alone does not determine the language-level result of overflow. Hardware commonly retains the low-order (n) bits, but a language may classify signed overflow as an error, an exceptional condition, undefined behavior, or modular wraparound. These choices concern the semantics assigned to an operation rather than the encoding of its operands.
Bitwise operations act directly on the stored representation. Under two's complement, the all-one word denotes (-1), and bitwise complementation satisfies
[ \mathord{\sim}x=-x-1 ]
within a fixed width. This relation is a direct consequence of the additive-inverse identity and does not hold with the same interpretation in every signed-number representation.
Comparison with other signed representations
In sign-and-magnitude notation, the most significant bit records the sign while the remaining positions encode an absolute value. This arrangement provides symmetric positive and negative ranges but assigns separate patterns to positive and negative zero. Arithmetic must account explicitly for operand signs and magnitude comparisons.
Ones' complement forms a negative value by inverting every bit of its positive counterpart. Addition requires an end-around carry when a carry leaves the most significant position, and zero again has two encodings. The representation remains useful in specific forms of checksum arithmetic, where end-around carry is part of the intended computation.
Two's complement instead treats the complete word as a residue modulo (2^n). Its asymmetrical range, unique zero, and direct correspondence between signed and unsigned modular addition all follow from that mathematical structure rather than from independent encoding conventions.