Overflow
Overflow has occurred when the result of an addition or subtraction has given us a result with the wrong sign. This happens when the result of the computation cannot fit within the bounds of representation. Overflow is not the same as carry out, but they are related.
Consider 8-bit signed integer addition under two’s complement representation. Given this representation and bit width, we can only represent integers in the interval [-128, 127].
Now consider what happens if we add 127 + 1.
\begin{align*} &\phantom{+\;}0111{\;}1111 \\ &+\;0000{\;}0001 \\ &\rule{2cm}{0.5pt}\\ &\phantom{+\;}1000{\;}0000 \end{align*}
That’s 127 + 1 = -128! The result has the wrong sign. This is overflow—we’ve tried to represent a number that’s too big to fit within the given bit width.
Notice also that no carry out occurs in this operation. Again, carry out is not the same as overflow.
We can have carry out with overflow. Here’s one example: -128 + (-128).
\begin{align*} &\phantom{+\;}1000{\;}0000 \\ &+\;1000{\;}0000 \\ &\rule{2cm}{0.5pt}\\ 1{\;}&\phantom{+\;}0000{\;}0000 \end{align*}
The operation -128 + (-128) without restriction would give us -256, but we can’t represent -256 with eight bits under two’s complement. There’s a carry out of one, and the sign of the result is wrong—zero is not negative.
Here’s an operation that has carry in to the most significant bit, and carry out from the most significant bit, but no overflow: -16 + (-16).
\begin{align*} &\phantom{+\;}1111{\;}0000 \\ &+\;1111{\;}0000 \\ &\rule{2cm}{0.5pt}\\ 1{\;}&\phantom{+\;}1110{\;}0000 \end{align*}
-16 + (-16) = -32 just as expected. No overflow; sign is correct.
So, if we want to detect overflow, we XOR the last two carry outs. With 8-bits, in this example,
\text{Overflow flag} = c_8 \oplus c_7
or generally, for n-bits
\text{Overflow flag} = c_n \oplus c_{n - 1}.
© 2025 Clayton Cafiero.
No generative AI was used in writing this material. This was written the old-fashioned way.