Two's complement

Two's complement is the most common method of representing signed (positive, negative, and zero) integers on computers,[1] and more generally, fixed point binary values.

Compared to other systems for representing signed numbers (e.g., ones' complement), the two's complement has the advantage that the fundamental arithmetic operations of addition, subtraction, and multiplication are identical to those for unsigned binary numbers (as long as the inputs are represented in the same number of bits as the output, and any overflow beyond those bits is discarded from the result).

[4] The method of complements had long been used to perform subtraction in decimal adding machines and mechanical calculators.

John von Neumann suggested use of two's complement binary representation in his 1945 First Draft of a Report on the EDVAC proposal for an electronic stored-program digital computer.

[5] The 1949 EDSAC, which was inspired by the First Draft, used two's complement representation of negative binary integers.

The IBM 700/7000 series scientific machines use sign/magnitude notation, except for the index registers which are two's complement.

Early commercial computers storing negative values in two's complement form include the English Electric DEUCE (1955) and the Digital Equipment Corporation PDP-5 (1963) and PDP-6 (1964).

In two's complement notation, a non-negative number is represented by its ordinary binary representation; in this case, the most significant bit is 0.

To get the two's complement of a negative binary number, all bits are inverted, or "flipped", by using the bitwise NOT operation; the value of 1 is then added to the resulting value, ignoring the overflow which occurs when taking the two's complement of 0.

To convert to −5 in two's-complement notation, first, all bits are inverted, that is: 0 becomes 1 and 1 becomes 0: At this point, the representation is the ones' complement of the decimal value −5.

Hence, in the unsigned binary arithmetic the value of two's-complement negative number x* of a positive x satisfies the equality x* = 2N − x.

[a] For example, to find the four-bit representation of −5 (subscripts denote the base of the representation): Hence, with N = 4: The calculation can be done entirely in base 10, converting to base 2 at the end: A shortcut to manually convert a binary number into its two's complement is to start at the least significant bit (LSB), and copy all the zeros, working from LSB toward the most significant bit (MSB) until the first 1 is reached; then copy that 1, and flip all the remaining bits (Leave the MSB as a 1 if the initial number was in sign-and-magnitude representation).

Similarly, when a number is shifted to the right, the most-significant bit, which contains the sign information, must be maintained.

However, if the most-significant bit changes from 0 to 1 (and vice versa), overflow is said to occur in the case that the value represents a signed integer.

Note that unlike addition and subtraction, width extension and right shifting are done differently for signed and unsigned numbers.

Note that the two's complement being the same number is detected as an overflow condition since there was a carry into but not out of the most-significant bit.

For example, In the C and C++ programming languages, the above behaviours are undefined and not only may they return strange results, but the compiler is free to assume that the programmer has ensured that undefined numerical operations never happen, and make inferences from that assumption.

modulo 256. is equivalent to 161. since Fundamentally, the system represents negative integers by counting backward and wrapping around.

Negating a two's complement number is simple: Invert all the bits and add one to the result.

However, the hardware can simply ignore the left-most bit to give the correct answer of 0010 (2.).

For example, adding 15 and −5: Or the computation of 5 − 15 = 5 + (−15): This process depends upon restricting to 8 bits of precision; a carry to the (nonexistent) 9th most significant bit is ignored, resulting in the arithmetically correct result of 1010.

The last two bits of the carry row (reading right-to-left) contain vital information: whether the calculation resulted in an arithmetic overflow, a number too large for the binary system to represent (in this case greater than 8 bits).

Conveniently, an XOR operation on these two bits can quickly determine if an overflow condition exists.

Using complements for subtraction is closely related to using complements for representing negative numbers, since the combination allows all signs of operands and results; direct subtraction works with two's-complement numbers as well.

Then the numbers are multiplied, discarding the bits beyond the eighth bit (as shown by "x"): This is very inefficient; by doubling the precision ahead of time, all additions must be double-precision and at least twice as many partial products are needed than for the more efficient algorithms actually implemented in computers.

In the following example, again multiplying 6 by −5, the two registers and the extended sign bit are separated by "|": Comparison is often implemented with a dummy subtraction, where the flags in the computer's status register are checked, but the main result is ignored.

The following algorithm (for an n-bit two's complement architecture) sets the result register R to −1 if A < B, to +1 if A > B, and to 0 if A and B are equal: In a classic HAKMEM published by the MIT AI Lab in 1972, Bill Gosper noted that whether or not a machine's internal representation was two's-complement could be determined by summing the successive powers of two.

"[16] Gosper's end conclusion is not necessarily meant to be taken seriously, and it is akin to a mathematical joke.

This presupposes a method by which an infinite string of 1s is considered a number, which requires an extension of the finite place-value concepts in elementary arithmetic.

[17] Digital arithmetic circuits, idealized to operate with infinite (extending to positive powers of 2) bit strings, produce 2-adic addition and multiplication compatible with two's complement representation.