Sub topic 3
Signed integers: sign-magnitude, 1's and 2's complement
What this chapter assumes
You should know unsigned binary place value, how to add binary numbers by hand, and powers of two. The previous chapter gave the unsigned range of n bits as 0 to 2^n - 1. This chapter shows three ways to spend those same n bits on both positive and negative values, and why one of them won.
The design problem
An n-bit pattern has 2^n possibilities. A signed format assigns some of them to negative numbers. A good format should meet four goals:
- Each value has one pattern.
- The hardware can reuse the unsigned adder.
- Negation is cheap.
- The sign is easy to read.
No format is perfect. The three below differ in how well they meet these goals.
Sign-magnitude
The most significant bit (MSB) is the sign: 0 means positive, 1 means negative. The remaining n - 1 bits hold the absolute value as an unsigned number.
- +5 in 4 bits is
0101, and -5 is1101. - Negation flips only the sign bit.
- There are two zeros:
0000(+0) and1000(-0). - The range is -(2^(n-1) - 1) to +(2^(n-1) - 1), which is 2^n - 1 distinct values.
The drawback is arithmetic. To add two numbers, the hardware must compare signs, compare magnitudes, and choose between adding and subtracting. The plain unsigned adder cannot be reused.
1's complement
A non-negative number is stored as plain binary. A negative number -x is stored by inverting every bit of the pattern for x.
- +5 is
0101, so -5 is1010. - Negation is a bitwise NOT.
- There are still two zeros:
0000and1111. - The range is the same as sign-magnitude: -(2^(n-1) - 1) to +(2^(n-1) - 1).
Numerically, the pattern for -x is 2^n - 1 - x. Addition works with one extra rule, the end-around carry. If the addition produces a carry out of the MSB, add that carry back into the least significant bit.
Example in 4 bits, 5 + (-3). Here -3 is 1100.
0101 + 1100 = 1 0001, a 5-bit result with carry 1.- Add the carry back:
0001 + 1 = 0010, which is 2.
The end-around carry is the cost of the second zero.
2's complement
A non-negative number is stored as plain binary. A negative number -x is stored as the pattern for 2^n - x. Equivalently, invert all bits and add 1.
There is a second way to read it. Give the MSB the weight -2^(n-1) and every other bit its usual positive weight. Then 1011 in 4 bits is -8 + 0 + 2 + 1 = -5.
- There is exactly one zero,
0000. - The range is -2^(n-1) to +2^(n-1) - 1, which is all 2^n patterns.
- The range is asymmetric. There is one more negative value than positive.
Negating by hand
Two methods give the same result. The second is quicker on paper.
For example, take 0110 (+6). The first 1 from the right is the second bit. Copy 10 and invert the left part 01 to 10. The result is 1010, which is -8 + 2 = -6.
Side-by-side comparison
This table shows all 4-bit patterns. A dash means the value cannot be represented.
| Value | Sign-magnitude | 1's complement | 2's complement |
|---|---|---|---|
| +7 | 0111 | 0111 | 0111 |
| +1 | 0001 | 0001 | 0001 |
| 0 | 0000 | 0000 | 0000 |
| -0 | 1000 | 1111 | - |
| -1 | 1001 | 1110 | 1111 |
| -4 | 1100 | 1011 | 1100 |
| -5 | 1101 | 1010 | 1011 |
| -7 | 1111 | 1000 | 1001 |
| -8 | - | - | 1000 |
| Property | Sign-magnitude | 1's complement | 2's complement |
|---|---|---|---|
| Range for n bits | -(2^(n-1) - 1) to 2^(n-1) - 1 | -(2^(n-1) - 1) to 2^(n-1) - 1 | -2^(n-1) to 2^(n-1) - 1 |
| Zeros | 2 | 2 | 1 |
| Distinct values | 2^n - 1 | 2^n - 1 | 2^n |
| Negation | Flip sign bit | Invert all bits | Invert and add 1 |
| Reuses unsigned adder | No | With end-around carry | Yes, unchanged |
Why 2's complement wins
In 2's complement, the pattern for -x is 2^n - x. Arithmetic on n-bit patterns is arithmetic modulo 2^n. Adding the patterns of a and -b gives a + 2^n - b. Discarding the carry out of bit n - 1 subtracts 2^n, which leaves a - b. The same adder handles unsigned and signed operands, and subtraction becomes addition of the negated operand.
Example in 4 bits, 5 + (-3) with -3 as 1101:
0101 + 1101 = 1 0010. Drop the carry and the result is 0010, which is 2.
Encoding and decoding in code
This code encodes a value as an n-bit 2's complement pattern and decodes it again. It also shows all three encodings of the same number.
def to_twos(x, n):
lo, hi = -(1 << (n - 1)), (1 << (n - 1)) - 1
if not lo <= x <= hi:
raise OverflowError(f"{x} does not fit in {n} bits (range {lo}..{hi})")
return x & ((1 << n) - 1)
def from_twos(bits, n):
if not 0 <= bits < (1 << n):
raise ValueError("pattern does not fit in n bits")
if (bits >> (n - 1)) & 1:
return bits - (1 << n)
return bits
def all_three(x, n):
mask = (1 << n) - 1
mag = abs(x)
sign_mag = ((1 if x < 0 else 0) << (n - 1)) | mag
ones = x if x >= 0 else (~mag) & mask
twos = x & mask
return sign_mag, ones, twos
print(format(to_twos(-5, 8), '08b')) # 11111011
print(format(to_twos(5, 8), '08b')) # 00000101
print(from_twos(0b11111011, 8)) # -5
print([format(p, '04b') for p in all_three(-5, 4)]) # ['1101', '1010', '1011']Python integers have unlimited width, so x & mask is what forces the n-bit pattern. For a negative x, the mask keeps the low n bits of its infinite 1's prefix, which is exactly the 2's complement pattern.
Sign extension
To widen a 2's complement number, copy the sign bit into the new high bits. This preserves the value.
1011(-5, 4 bits) becomes11111011(8 bits). Check: -128 + 64 + 32 + 16 + 8 + 2 + 1 = -5.0101(+5) becomes00000101.
Padding with zeros is correct only for unsigned values. Zero-extending 1011 gives 00001011, which is +11.
Sign extension works because each added 1 above the sign bit contributes -2^k + 2^(k-1) + ... and these terms cancel to leave the original value.
Common errors and edge cases
- Negating the most negative value. In 4 bits, -8 is
1000. Inverting gives0111, and adding 1 gives1000again. There is no +8, so the negation overflows to itself. The same thing happens toabs()in many languages. - Reading the carry as overflow. In 2's complement, the carry out of the MSB does not signal overflow. Signed overflow occurs when two operands of the same sign give a result of the opposite sign. Equivalently, it occurs when the carry into the MSB differs from the carry out of it. Adding
0111 + 0001gives1000: no carry out, but overflow. Adding1111 + 1111gives1 1110: carry out, but no overflow, since -1 + -1 = -2. - Applying the 2's complement range to the other formats. The range -2^(n-1) to 2^(n-1) - 1 belongs only to 2's complement. The other two give a symmetric range with 2^n - 1 values.
- Forgetting the end-around carry when checking 1's complement addition by hand.
- Decoding without knowing the format. The same pattern
1011means -3 in sign-magnitude, -4 in 1's complement and -5 in 2's complement. The bits carry no format information.
GATE-style shortcuts
- Counting values: n bits give 2^n patterns. 1's complement and sign-magnitude lose one pattern to the duplicate zero.
- The smallest 2's complement value is the pattern
100...0. The largest is011...1. - If a question asks for the 2's complement of a number in hex, convert to binary, apply the rightmost-1 rule, and convert back.
- To compare formats, write out a 3-bit or 4-bit table. A small table exposes duplicate zeros and the asymmetric range faster than formulas.
Exercise
An 8-bit register holds the hexadecimal pattern B4. What decimal value does it represent if it is read as (a) unsigned, (b) sign-magnitude, (c) 1's complement, (d) 2's complement?
Show answer
B4 is 10110100. (a) Unsigned: 128 + 32 + 16 + 4 = 180. (b) Sign-magnitude: the sign bit is 1 and the magnitude is 0110100 = 52, so the value is -52. (c) 1's complement: invert to 01001011 = 75, so the value is -75. (d) 2's complement: 180 - 256 = -76.
Exercise
A 6-bit machine uses 1's complement. How many distinct integer values can it represent, and what is its range? Then give the 6-bit 2's complement pattern of -23 and use it to check that adding +23 gives zero.
Show answer
1's complement has two zeros (000000 and 111111), so there are 2^6 - 1 = 63 distinct values, in the range -31 to +31. For -23: +23 is 010111. Rightmost-1 rule: copy 1, then invert the left part 01011 to 10100, giving 101001. Check: 010111 + 101001 = 1 000000. Dropping the carry leaves 000000, which is zero.