Sub topic 4
2's Complement Arithmetic and Overflow Detection
Assume that you can add binary numbers, use powers of two, and convert between binary and decimal. This chapter uses fixed-width integers, so every result must fit into a chosen number of bits.
How 2's complement arithmetic works
For an \(n\)-bit 2's complement integer, the bit weights are:
- The rightmost bit has weight \(2^0\).
- Each next bit has weight \(2^1, 2^2,\ldots\).
- The leftmost bit has weight \(-2^{n-1}\).
Therefore, the signed range is:
\[ -2^{n-1} \text{ through } 2^{n-1}-1 \]
For 4 bits, the range is -8 through 7.
| Bits | Value |
|---|---|
| 0101 | 5 |
| 1101 | -3 |
| 1000 | -8 |
To find the negative of a number, invert every bit and add 1. For example:
\[ 0011 = 3 \]
\[ \text{invert: } 1100,\quad \text{add 1: } 1101 = -3 \]
The same binary adder can perform both addition and subtraction. To calculate \(A-B\), add \(A\) to the 2's complement representation of \(B\):
\[ A-B=A+(\text{invert}(B)+1) \]
Any carry beyond the fixed width is discarded.
Addition and overflow
2's complement addition follows ordinary binary addition. The important question is whether the mathematical result is outside the signed range.
Overflow can occur only when:
- Two positive numbers produce a negative result.
- Two negative numbers produce a positive result.
Adding numbers with different signs cannot produce signed overflow.
Example: positive overflow
Using 4 bits:
0111 (+7)
+ 0011 (+3)
------
1010 (-6 as a 4-bit signed value)The mathematical result is +10, but +10 is outside the 4-bit range -8 through 7. The result is therefore invalid because overflow occurred.
Example: negative overflow
1011 (-5)
+ 1100 (-4)
------
1 0111 (+7 after discarding carry)The mathematical result is -9, which is below -8. The stored 4-bit result is incorrect because overflow occurred.
A carry out of the most significant bit does not by itself indicate overflow. For example:
1101 (-3)
+ 0011 (+3)
------
1 0000 (0 after discarding carry)There is a carry out, but the signs differ and the result is valid.
Detecting overflow with sign bits
Let \(sA\), \(sB\), and \(s_R\) be the sign bits of the two inputs and the result. The overflow condition is:
\[ (sA=sB)\ \text{and}\ (sR\ne sA) \]
An equivalent hardware rule uses carries:
\[ V=C{\text{into sign bit}}\oplus C{\text{out of sign bit}} \]
where \(\oplus\) means exclusive OR.
def add_twos(a, b, bits):
mask = (1 << bits) - 1
raw = (a + b) & mask
sign = 1 << (bits - 1)
value = raw - (1 << bits) if raw & sign else raw
a_sign = (a >> (bits - 1)) & 1
b_sign = (b >> (bits - 1)) & 1
r_sign = (raw >> (bits - 1)) & 1
overflow = a_sign == b_sign and r_sign != a_sign
return value, overflow
print(add_twos(7, 3, 4)) # (-6, True)
print(add_twos(-3, 3, 4)) # (0, False)GATE shortcut
For an \(n\)-bit signed result:
- Compute the binary sum and keep only \(n\) bits.
- Check the signs of both operands.
- If the operand signs differ, overflow is impossible.
- If the signs match and the result sign changes, overflow occurred.
- Otherwise, the result is valid.
Exercise
Using 4-bit 2's complement arithmetic, compute 6 - (-3) and determine whether overflow occurs.
Show answer
Convert -3 to 1101. Its negation is 0011, so 6 - (-3) = 0110 + 0011 = 1001. The mathematical result is 9, outside the range -8 through 7, so 1001 represents -7 and signed overflow occurred.