TOPICS
Search

Two's Complement


Two's complement is a fixed-width encoding of integers. With n bits, a value x in the range -2^(n-1)<=x<=2^(n-1)-1 is encoded by the ordinary n-digit binary representation of the residue of x modulo 2^n. A negative integer -x can therefore be encoded by taking the ones' complement of the bit string for x and then performing addition of 1. For example, the 8-bit representation of -11 is (11110101)_2.

Two's complement has a single representation of zero. Its asymmetric range contains -2^(n-1) but not 2^(n-1). addition is performed modulo 2^n, so a carry beyond the leading digit is discarded. For example, the calculation for 17-11=6 is

 (00010001)_2+(11110101)_2=(100000110)_2->(00000110)_2=6.

The numeral spelling "2's complement" is common in engineering. The hyphenated form "two's-complement" is used attributively, as in "two's-complement representation," while the unpunctuated spelling "twos complement" occurs in identifiers or typography that omits apostrophes. These forms denote the same representation.


See also

Binary, Binary Expansion, Bit, Carry, Encoding, Negabinary, Ones' Complement, Sign-and-Magnitude Representation, Zero

Explore with Wolfram|Alpha

References

Knuth, D. E. "Positional Number Systems." §4.1 in The Art of Computer Programming, Vol. 2: Seminumerical Algorithms, 3rd ed. Reading, MA: Addison-Wesley, p. 203, 1998.

Cite this as:

Weisstein, Eric W. "Two's Complement." From MathWorld--A Wolfram Resource. https://mathworld.wolfram.com/TwosComplement.html

Subject classifications