Sub topic 4
2's Complement Arithmetic and Overflow Detection
This chapter assumes you can already write an n-bit 2's complement number and know its range, -2^(n-1) to 2^(n-1)-1. It also assumes you can add binary numbers by hand. The chapter covers how to add and subtract in 2's complement, and how to tell when a result does not fit.
Why one adder is enough
2's complement lets the hardware use the same adder for signed and unsigned operands. The adder sees only bit patterns. Whether a pattern means 250 or -6 is decided by how you read the result.
The addition rule is short:
- Add the two n-bit patterns as if they were unsigned.
- Discard any carry out of the most significant bit (MSB).
- Read the n-bit result as 2's complement.
Worked example in 8 bits, 5 + (-3):
00000101 (+5)
+ 11111101 (-3)
----------
1 00000010 carry out discarded, result = 00000010 = +2The rule works because an n-bit pattern behaves like arithmetic modulo 2^n. The pattern for -3 is 2^8 - 3 = 253. Adding 5 gives 258, and 258 mod 256 = 2.
Negation and subtraction
To negate a number, invert every bit and add 1. Check with 5 in 8 bits: 00000101 becomes 11111010, and adding 1 gives 11111011, which is -5.
There is also a shortcut for hand work. Copy the bits from the right up to and including the first 1, then invert all the bits to its left. For 00101100 the right part "100" is copied and the rest is inverted, so you get 11010100.
Subtraction reuses the adder:
A - B = A + (~B) + 1
The hardware inverts B and sets the adder's carry-in to 1. In a combined adder/subtractor, a control bit decides whether B is inverted and also feeds the carry-in.
Worked example in 4 bits, 6 - 3:
0110 A = 6
1100 ~B (B = 0011)
1 carry-in
-----
10011 discard carry, result = 0011 = 3The most negative number
Negating -2^(n-1) gives the same pattern back. In 4 bits, -8 is 1000. Inverting gives 0111, and adding 1 gives 1000 again. The value +8 does not exist in 4 bits, so this is an overflow case. The range has one more negative value than positive values.
Sign extension
To widen a 2's complement number, copy the sign bit into the new high bits. For example, 1101 (-3) becomes 11111101 (-3) in 8 bits, and 0101 (+5) becomes 00000101. Zero-extension, which fills with 0s, is only correct for unsigned values.
What overflow means
Overflow happens when the true result lies outside -2^(n-1) to 2^(n-1)-1, so the n-bit pattern shows the wrong value. Adding two n-bit numbers can need n+1 bits.
Sign rules tell you exactly when this can happen:
| Operation | Operand signs | Overflow possible? | Overflow shows as |
|---|---|---|---|
| A + B | both positive | yes | negative result |
| A + B | both negative | yes | positive result |
| A + B | different signs | never | none |
| A - B | A positive, B negative | yes | negative result |
| A - B | A negative, B positive | yes | positive result |
| A - B | same signs | never | none |
Adding numbers of opposite signs moves the result toward zero, so it always fits. Subtraction A - B is addition of A and -B, so the same reasoning applies to the sign of -B.
Detecting overflow in hardware
There are two equivalent tests.
Sign test. Let a, b and r be the sign bits of the operands and the result in an addition:
V = (a = b) AND (r ≠ a)
Carry test. Let Cin be the carry into the MSB position and Cout the carry out of it:
V = Cin XOR Cout
The carry test is cheap in hardware because both carries already exist inside the adder. It is also the form most often asked in GATE.
Worked examples in 4 bits (range -8 to 7)
7 + 5: 0111 + 0101 = 1100.
- The result reads as -4, but the true sum is 12, so this is an overflow.
- Cin = 1, because bit 2 produces a carry into bit 3. Cout = 0. V = 1 XOR 0 = 1.
-8 + -1: 1000 + 1111 = 1 0111.
- After discarding the carry the result is 0111, which is +7, but the true sum is -9, so this is an overflow.
- Cin = 0 and Cout = 1. V = 1.
3 + (-3): 0011 + 1101 = 1 0000.
- The result is 0, which is correct.
- Cin = 1 and Cout = 1. V = 0, even though a carry came out of the MSB.
Carry is not overflow
Real CPUs keep separate flags for this. x86 has CF for unsigned overflow and OF for signed overflow. The same addition instruction sets both, and the program picks the flag that matches how it interprets the data.
For subtraction, the carry flag has an extra twist. Because A - B is computed as A + ~B + 1, a carry out of 1 means no borrow occurred, so many architectures report borrow as the inverse of carry. Check the problem statement if it asks about "borrow".
A runnable model
The function below models an n-bit adder and returns the result pattern, C_out, and the overflow flag computed from the carry test.
def add_n(a, b, n, cin=0):
mask = (1 << n) - 1
a &= mask
b &= mask
total = a + b + cin
result = total & mask
carry_out = total >> n
low = (1 << (n - 1)) - 1
carry_in_msb = ((a & low) + (b & low) + cin) >> (n - 1)
return result, carry_out, carry_in_msb ^ carry_out
def sub_n(a, b, n):
mask = (1 << n) - 1
return add_n(a, ~b & mask, n, 1)
def to_signed(x, n):
return x - (1 << n) if x >> (n - 1) else x
def show(label, triple, n):
r, c, v = triple
print(label, "value", to_signed(r, n), "carry", c, "overflow", v)
show("127 + 1 ", add_n(127, 1, 8), 8) # value -128 carry 0 overflow 1
show("-128 + -1 ", add_n(-128, -1, 8), 8) # value 127 carry 1 overflow 1
show("100 + -50 ", add_n(100, -50, 8), 8) # value 50 carry 1 overflow 0
show("5 - 7 ", sub_n(5, 7, 8), 8) # value -2 carry 0 overflow 0The third line shows a carry with no overflow, and the first shows an overflow with no carry. The fourth line has carry 0 because subtraction computes 5 + ~7 + 1 and no carry comes out, which is the "borrow occurred" case.
Common mistakes
- Reading overflow from the carry out alone. Use Cin XOR Cout or the sign test.
- Calling a correct wrapped result an error. If the operand signs differ, the result is always valid.
- Forgetting the +1 in subtraction. Inverting B alone gives the 1's complement, which is off by one.
- Zero-extending a negative number. This changes its value. Use sign extension.
- Testing overflow after widening. Detect it at the original width n, or widen both operands first so the sum fits.
GATE-style checklist
- Write both operands in n bits. Pad with sign extension if needed.
- Add the patterns and drop the carry out.
- Check the operand signs. If they differ, there is no overflow. If they match, compare the result sign with them.
- Convert the result back to decimal only if the question asks for the value.
Exercise
Add 01101100 and 01010100 as 8-bit 2's complement numbers. Give the result pattern, the carry into the MSB, the carry out of the MSB, and whether overflow occurs.
Show answer
The sum is 11000000. The carry into the MSB is 1 and the carry out is 0, so V = 1 XOR 0 = 1 and overflow occurs. Both operands are positive (108 and 84) and the result looks negative (-64). The true sum 192 exceeds the maximum 127.
Exercise
In 4-bit 2's complement, compute 6 - (-7) as 6 + ~(-7) + 1. Give the result pattern and say whether overflow occurs.
Show answer
-7 is 1001, and ~1001 = 0110. Then 0110 + 0110 + 1 = 1101, which reads as -3. The carry into the MSB is 1 and the carry out is 0, so V = 1 and overflow occurs. The true result 13 is above the maximum 7, and a positive minus a negative gave a negative-looking result.