Sub topic 3
Signed Integers: Sign-Magnitude, 1's and 2's Complement
This chapter assumes that you know binary place value, basic binary addition, and powers of two. A computer uses a fixed number of bits, so it needs a rule for interpreting one bit pattern as either a positive or a negative integer.
The sign bit
For an n-bit signed representation, the most significant bit is often called the sign bit:
0indicates a nonnegative value.1indicates a negative value.
The remaining n - 1 bits store the magnitude or part of the value. Three common encodings are sign-magnitude, 1's complement, and 2's complement.
| Representation | Negative number construction | Range for `n` bits | Number of zeros |
|---|---|---|---|
| Sign-magnitude | Set sign bit to `1` | `-(2^(n-1)-1)` to `2^(n-1)-1` | Two |
| 1's complement | Invert every bit of the positive value | `-(2^(n-1)-1)` to `2^(n-1)-1` | Two |
| 2's complement | Invert every bit and add `1` | `-2^(n-1)` to `2^(n-1)-1` | One |
Sign-magnitude representation
In sign-magnitude, the first bit stores the sign and the remaining bits store the absolute value.
For 8 bits:
00000101represents+5.10000101represents-5.
The value range is from -127 to +127. Both 00000000 and 10000000 represent zero. The second zero is called negative zero.
Sign-magnitude is easy to understand, but arithmetic requires separate handling for the sign and magnitude. This makes hardware more complicated.
1's complement representation
To represent a negative value in 1's complement, write the positive value and invert every bit.
For 8 bits:
+5: 00000101
-5: 11111010To decode a negative pattern, invert every bit and attach a negative sign:
11111010 -> 00000101 -> -5Like sign-magnitude, 1's complement has two zeros:
00000000 = +0
11111111 = -0Its range is also -127 to +127. Addition may produce an end-around carry, where a carry from the most significant bit is added back to the least significant bit.
2's complement representation
2's complement is the standard representation used by modern computers. To form a negative number:
- Write the positive value.
- Invert every bit.
- Add
1.
For 8 bits:
+5: 00000101
invert: 11111010
add 1: 11111011Therefore, 11111011 represents -5.
To decode a negative 2's complement pattern, invert it and add 1, then attach a negative sign. The 8-bit range is -128 to +127. It has only one zero because the bit pattern that would represent negative zero is used for -128.
Comparing the encodings
The most important differences are the range and zero representation. For n bits, 2's complement provides one additional negative value because it uses the negative-zero pattern.
def twos_complement_bits(x, n):
lower = -(1 << (n - 1))
upper = (1 << (n - 1)) - 1
if not lower <= x <= upper:
raise OverflowError(f"{x} does not fit in {n} bits")
return format(x & ((1 << n) - 1), f"0{n}b")
print(twos_complement_bits(-5, 8)) # 11111011Exercise
Write -13 in 8-bit sign-magnitude, 1's complement, and 2's complement. Which representation has the widest range?
Show answer
+13 is 00001101. The representations are sign-magnitude 10001101, 1's complement 11110010, and 2's complement 11110011. 2's complement has the widest range: -128 to 127.