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 -bit two's-complement integer, the most-significant bit has weight . The remaining weights are .
For eight bits the weights are . Decode as . Decode as . 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 . The maximum is 01111111, giving 127. There are still 256 different patterns, but they now include negatives rather than extending to 255.
An -bit two's-complement integer has range to , inclusive.
The range is asymmetric because there is one zero and one more negative value than positive value. Four bits cover to 7; six bits cover 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 in eight bits, write . Check first that the value fits the signed range. Although fits eight unsigned bits, it does not fit eight signed two's-complement bits.
To represent , encode at the chosen width, invert every bit, then add one:
The inversion alone is not the two's-complement negative. The final increment is essential. Checking the signed weights confirms . The same process changes a representable negative value to its positive opposite. For , invert to 00000101 and add one to obtain 00000110, so the original represents .
An alternative encoding method uses the negative weight directly. For , begin with and find the positive remainder 103. Since , 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 ; eight-bit 11111101 is also . Padding with zeros would produce 00001101, or , and change the value.
The most-negative value has no positive opposite within the same width. In eight bits, is representable but 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 , so the same binary addition process can perform both operations.
For in eight bits, encode 23 as 00010111 and 9 as 00001001. Invert the latter and add one to get , or 11110111. Adding gives . Discard the carry beyond the eight-bit field and decode the retained bits as 14. The decimal check is .
For a negative answer, consider . Add 00001100 to the representation of , 11101100, giving 11111000. Its signed weights give . 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 gives . The mathematical sum 9 is above the signed maximum 7, and 1001 decodes as : signed overflow has occurred even though there is no carry beyond the fourth bit. Conversely, four-bit gives ; 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.