p14m

Overflow Detection

How hardware knows when an arithmetic result doesn’t fit.

Problem

When two N-bit numbers are added, the true mathematical result might need N+1 bits. How does hardware detect this?

How it works

Unsigned overflow is simple: the carry-out of the MSB position is 1. This means the result exceeds 2N−1.

Signed overflow is different. It occurs when the result’s sign is wrong: specifically when both operands have the same sign but the result has the opposite sign. The rule:

Overflow = Cin[MSB] ⊕ Cout[MSB]

That is, overflow happens when the carry into the sign bit differs from the carry out of it. Equivalently:
• (+) + (+) = (−) → overflow
• (−) + (−) = (+) → overflow
• Mixed signs → never overflows

Hardware implements this with a single XOR gate on the two carry signals. CPUs set the overflow flag (V or OF) using exactly this logic.
Key insightSigned overflow is a single XOR of two carry bits. The adder already produces both carries, so detection is essentially free.