4.5.4.3 - Signed binary and two's complement

4.5.4.3 - Signed binary and two's complement

You will encode and decode positive and negative integers using two's complement, calculate its range, and use addition to perform subtraction. The interpretation of the most-significant bit is what makes a negative value possible.

Signed ranges and place values

Two's complement assigns a negative weight to the leftmost bit while retaining positive weights for the remaining positions.

Two's complement

For an nn-bit two's-complement integer, the most-significant bit has weight −2n−1-2^{n-1}. The remaining weights are 2n−2,…,21,202^{n-2},\ldots,2^1,2^0.

For eight bits the weights are −128,64,32,16,8,4,2,1-128,64,32,16,8,4,2,1. Decode 1110011111100111 as −128+64+32+4+2+1=−25-128+64+32+4+2+1=-25. Decode 0001100100011001 as 16+8+1=2516+8+1=25. A leading 0 indicates a non-negative value; a leading 1 indicates a negative value. The leading bit is not a separate minus sign attached to an unsigned magnitude: 11100111 does not mean “minus 103”.

The minimum pattern is 10000000, giving −128-128. The maximum is 01111111, giving 127. There are still 256 different patterns, but they now include negatives rather than extending to 255.

An nn-bit two's-complement integer has range −2n−1-2^{n-1} to 2n−1−12^{n-1}-1, inclusive.

The range is asymmetric because there is one zero and one more negative value than positive value. Four bits cover −8-8 to 7; six bits cover −32-32 to 31. Two's complement is the negative-integer representation examined in this specification.

Representing positive and negative integers

Encoding a positive value starts with ordinary binary, but the chosen signed width must leave its most-significant bit zero.

For +25+25 in eight bits, write 0001100100011001. Check first that the value fits the signed range. Although 200200 fits eight unsigned bits, it does not fit eight signed two's-complement bits.

To represent −25-25, encode +25+25 at the chosen width, invert every bit, then add one:

00011001 ⟶ 11100110 ⟶ 11100111.00011001\ \longrightarrow\ 11100110\ \longrightarrow\ 11100111.

The inversion alone is not the two's-complement negative. The final increment is essential. Checking the signed weights confirms 11100111=−2511100111=-25. The same process changes a representable negative value to its positive opposite. For 1111101011111010, invert to 00000101 and add one to obtain 00000110, so the original represents −6-6.

An alternative encoding method uses the negative weight directly. For −25-25, begin with −128-128 and find the positive remainder 103. Since 103=64+32+4+2+1103=64+32+4+2+1, the same eight-bit pattern is 11100111. Either method is valid.

To widen a signed value, sign-extend it: repeat the sign bit on the left. Four-bit 1101 is −3-3; eight-bit 11111101 is also −3-3. Padding with zeros would produce 00001101, or +13+13, and change the value.

The most-negative value has no positive opposite within the same width. In eight bits, −128-128 is representable but +128+128 is not. Invert-and-add-one applied to 10000000 produces 10000000 again after restricting to eight bits; that cannot be interpreted as a successful positive result.

Adding and subtracting signed values

Two's-complement subtraction uses the identity A−B=A+(−B)A-B=A+(-B), so the same binary addition process can perform both operations.

For 23−923-9 in eight bits, encode 23 as 00010111 and 9 as 00001001. Invert the latter and add one to get −9-9, or 11110111. Adding gives 1  000011101\;00001110. Discard the carry beyond the eight-bit field and decode the retained bits as 14. The decimal check is 23−9=1423-9=14.

For a negative answer, consider 12−2012-20. Add 00001100 to the representation of −20-20, 11101100, giving 11111000. Its signed weights give −8-8. A leading 1 is expected for this correct negative answer, rather than evidence of an error.

Discarding a carry is part of fixed-width signed addition, but you must still check signed overflow. When adding two positive values, a negative result cannot be correct. When adding two negative values, a non-negative result cannot be correct. Adding operands of opposite signs cannot overflow this signed range. For subtraction, apply that addition rule after replacing the subtracted value with its negative, or compare the mathematical result with the representable range.

For example, four-bit 5+45+4 gives 0101+0100=10010101+0100=1001. The mathematical sum 9 is above the signed maximum 7, and 1001 decodes as −7-7: signed overflow has occurred even though there is no carry beyond the fourth bit. Conversely, four-bit −2+3-2+3 gives 1110+0011=1  00011110+0011=1\;0001; discarding the carry leaves the correct value 1. Carry-out and signed overflow are different tests.

State the width and interpretation, form the negative operand correctly, add, and then check the signed range. A leading 1 or discarded carry alone does not prove signed overflow.