Two's complement is the most common method of representing signed (positive, negative, and zero) integers on computers, and more generally, fixed point binary values. As with the ones' complement and sign-magnitude systems, two's complement uses the most significant bit as the sign to indicate positive (0) or negative (1) numbers, and nonnegative numbers are given their unsigned representation (6 is 0110, zero is 0000); however, in two's complement, negative numbers are represented by taking the bit complement of their magnitude and then adding one (−6 is 1010). The number of bits in the representation may be increased by padding all additional high bits of negative or positive numbers with 1's or 0's, respectively, or decreased by removing additional leading 1's or 0's. Unlike the ones' complement scheme, the two's complement scheme has only one representation for zero, with room for one extra negative number (the range of a 4-bit number is −8 to +7). Furthermore, the same arithmetic implementations can be used on signed as well as unsigned integers and differ only in the integer overflow situations, since the sum of representations of a positive number and its negative is 0 (with the carry bit set).
Procedure The following is the procedure for obtaining the two's complement representation of a given negative number in binary digits:
Step 1: starting with the absolute binary representation of the number, with the leading bit being a sign bit; Step 2: inverting (or flipping) all bits – changing every 0 to 1, and every 1 to 0; Step 3: adding 1 to the entire inverted number, ignoring any overflow. Accounting for overflow will produce the wrong value for the result. For example, to calculate the decimal number −6 in binary from the number 6:
Step 1: +6 in decimal is 0110 in binary; the leftmost significant bit (the first 0) is the sign (just 110 in binary would be −2 in decimal). Step 2: flip all bits in 0110, giving 1001. Step 3: add the place value 1 to the flipped number 1001, giving 1010. To verify that 1010 indeed has a value of −6, add the place values together, but subtract the sign value from the final calculation. Because the most significant value is the sign value, it must be subtracted to produce the correct result: 1010 = −(1×23) + (0×22) + (1×21) + (0×20) = 1×−8 + 0 + 1×2 + 0 = −6.
Steps 2 and 3 together are a valid method to compute the additive inverse − n {\displaystyle -n} of any (positive or negative) integer n {\displaystyle n} where both input and output are in two's complement. An alternative to compute − n {\displaystyle -n} is to use subtraction 0 − n {\displaystyle 0-n} . See below for subtraction of integers in two's complement.
Theory
Two's complement is an example of a radix complement. The 'two' in the name refers to the number 2N - "two to the power of N", which is the value in respect to which the complement is calculated in an N-bit system (the only case where exactly 'two' would be produced in this term is N = 1, so for a 1-bit system, but these do not have capacity for both a sign and a zero). As such, the precise definition of the two's complement of an N-bit number is the complement of that number with respect to 2N. The defining property of being a complement to a number with respect to 2N is simply that the summation of this number with the original produce 2N. For example, using binary with numbers up to three bits (so N = 3 and 2N = 23 = 8 = 10002, where '2' indicates a binary representation), a two's complement for the number 3 (0112) is 5 (1012), because summed to the original it gives 23 = 10002 = 0112 + 1012. Where this correspondence is employed for representing negative numbers, it effectively means, using an analogy with decimal digits and a number-space only allowing eight non-negative numbers 0 through 7, dividing the number-space into two sets: the first four of the numbers 0 1 2 3 remain the same, while the remaining four encode negative numbers, maintaining their growing order, so making 4 encode −4, 5 encode −3, 6 encode −2 and 7 encode −1. A binary representation has an additional utility however, because the most significant bit also indicates the group (and the sign): it is 0 for the first group of non-negatives, and 1 for the second group of negatives. The tables at right illustrate this property.
Calculation of the binary two's complement of a positive number essentially means subtracting the number from the 2N. But as can be seen for the three-bit example and the four-bit 10002 (23), the number 2N will not itself be representable in a system limited to N bits, as it is just outside the N bits space (the number is nevertheless the reference point of the "Two's complement" in an N-bit system). Because of this, systems with maximally N-bits must break the subtraction into two operations: first subtract from the maximum number in the N-bit system, that is 2N−1 (this term in binary is actually a simple number consisting of 'all 1s', and a subtraction from it can be done simply by inverting all bits in the number also known as the bitwise NOT operation) and then adding the one. Coincidentally, that intermediate number before adding the one is also used in computer science as another method of signed number representation and is called a ones' complement (named that because summing such a number with the original gives the 'all 1s'). 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). This property makes the system simpler to implement, especially for higher-precision arithmetic. Additionally, unlike ones' complement systems, two's complement has no representation for negative zero, and thus does not suffer from its associated difficulties. Otherwise, both schemes have the desired property that the sign of integers can be reversed by taking the complement of its binary representation, but two's complement has an exception – the lowest negative, as can be seen in the tables.
… excerpt ends here. Continue reading the full article.

