Overflow: Carry vs Signed Overflow
The adder always computes the sum mod ; overflow just means the true answer didn't fit. For unsigned numbers the carry out of the top bit says so. For signed numbers it's when two numbers of the same sign produce the opposite sign, which the hardware spots as (carry into the top bit) XOR (carry out).
What the symbols mean
- Stored sum
The n bits the adder keeps.
- Carry out
The carry out of the top column, bit n of the true sum.
What the symbols mean
- Carry flag
1 when the unsigned sum didn't fit.
What the symbols mean
- Overflow flag
1 when the signed sum didn't fit.
- Carry into the top bit
The carry coming out of column n − 2 into the sign column.
- XOR
1 when the two carries differ.
Explanation
Overflow is simply what happens when the true answer of an addition is too big (or too negative) for the bits you have, so the result wraps around. The adder doesn't know or care: it always produces the sum mod , the remainder after dividing by , just like clock arithmetic. What changes is whether that remainder is the answer you wanted, and that depends on how you read the bits, unsigned or signed, which is the instruction's choice again. The formulas look cryptic, but they're two yes-or-no questions: asks "did a carry fall off the top?" and asks "did the carry into the top column disagree with the carry out of it?". Bear with ; there's a clean reason for it.
Unsigned: the carry out says it all. Two -bit unsigned numbers add up to at most , which needs bits. The adder keeps of them; the extra one is the carry out . So the true sum is , and the unsigned answer is wrong exactly when . In 8 bits, : the adder stores and sets .
Signed: same signs in, opposite sign out. The signed range is to . If the two numbers have opposite signs, their sum lies between them, so it always fits. Two positives add up to at most ; if that's past , it wraps into the negative half of the number wheel. Two negatives wrap the other way. So the rule is: signed overflow happens exactly when both operands have the same sign and the result has the other sign. In 8 bits, wraps to .
The two flags answer different questions. The hardware computes both on every add, because it doesn't know which reading you're using:
| 8-bit add | unsigned | signed | ||
|---|---|---|---|---|
| 0x10 + 0x20 = 0x30 | ✓ | ✓ | 0 | 0 |
| 0x64 + 0x32 = 0x96 | ✓ | gives ✗ | 0 | 1 |
| 0xC8 + 0x64 = 0x2C | gives ✗ | ✓ | 1 | 0 |
| 0x80 + 0x80 = 0x00 | gives ✗ | gives ✗ | 1 | 1 |
Why . Everything about the sign happens in the top column, which adds the two sign bits and the carry coming in from below. Check the three cases.
- Both operands positive (sign bits ): the column adds , so and the result's sign bit is . Overflow (a negative result) happens exactly when , that is when .
- Both negative (sign bits ): the column adds , so and the result's sign bit is . Overflow (a non-negative result) happens exactly when , again when .
- Opposite signs (sign bits ): the column adds . If that's with no carry; if it's , so . Either way , and , matching "opposite signs can't overflow."
So in every case, signed overflow is "the two carries differ": a single XOR gate on two wires the adder already has.
Wraparound can be a feature. Since the hardware does arithmetic mod , any calculation that only needs an answer mod is always right, overflow or not. The classic use is a free-running timer. A 16-bit millisecond counter wraps every s, but now - start in 16-bit unsigned arithmetic gives the elapsed time correctly even across the wrap, as long as less than one full lap has passed. Checksums and hash functions lean on the same fact.
In C, unsigned arithmetic is defined to wrap mod . Signed overflow is undefined behavior, so the compiler may assume it never happens, and careful code checks first (or uses a builtin like __builtin_add_overflow, which reads exactly these flags).
A common mistake. It's tempting to think the carry flag means signed overflow too, since both get called "overflow." They answer different questions. In 8 bits, sets the carry ( when read unsigned), yet the signed answer, , is perfect. And sets no carry, but the signed answer is garbage. Read the flag that matches how you're reading the bits.
See it
Worked examples
Add 0x5A and 0x3C in 8 bits (). Find the result, and , and say which reading of the result is correct.
- 1
Write both in binary, one hex digit per group: 0x5A and 0x3C .
A 16-bit timer counts milliseconds and wraps from back to . It reads when a task starts and when it ends. How long did the task take?
- 1
The firmware computes end − start in 16-bit unsigned arithmetic, which works mod .
Practice
Type your answer and check it. Solve them all to complete the lesson.
An 8-bit adder adds 0xF0 and 0x20. What are the carry flag and the overflow flag ? Enter , then .
In 8-bit signed arithmetic, what does give?
Which 8-bit addition overflows as signed but not as unsigned?
Adding two 8-bit numbers, the carry into bit 7 is 0 and the carry out of bit 7 is 1. What happened?
A 16-bit timer counts milliseconds and wraps from to . It reads when a motor starts and when it stops. The firmware computes stop − start in 16-bit unsigned arithmetic. How many milliseconds did the motor run?
A 32-bit counter of microseconds wraps around every µs. How many minutes is that? Give two decimal places.
An 8-bit unsigned counter starts at and has added to it times. What value does it end at, and how many of those additions set the carry flag? Enter both.
The hardest one here. Get it, and you've got this lesson.
In 8-bit arithmetic, holds ( signed, unsigned). For how many of the possible bytes does set the overflow flag ? For how many does it set the carry flag ? Enter both, first.
Where it's used
Every timer, sequence number and ring-buffer index in an operating system or network stack lives on a wheel of size , and comparisons like "is this packet newer?" are written as wraparound subtractions on purpose. Languages that check for overflow (Rust in debug builds, Swift always) read these same carry and overflow flags after each add, which costs almost nothing because the adder computes them anyway.