core22 min studyLesson 102 in Engineering

Arithmetic, flags, and three meanings of overflow

Calculate fixed-width sums and distinguish unsigned carry, signed overflow, and language-level arithmetic rules.

On this page

What you’ll understand

  • Interpret the same result bits as unsigned and signed integers.
  • Calculate x86 carry and signed-overflow conditions for an addition.
  • Explain why hardware wraparound does not define every language expression.

The bits 10000000 can denote the unsigned integer 128 or the signed two’s-complement integer minus 128 when the width is eight bits. Arithmetic operates on a specified representation; the meaning assigned to the result determines whether a range was exceeded. This lesson uses small numbers you can inspect, then connects them to actual x86 flags and the contrasting flag-free base RISC-V design.

Modulo arithmetic keeps the destination width

For a b-bit wrapping addition, keep the low b bits of the mathematical sum. Unsigned values represent zero through two to the b minus one, so a carry beyond the destination signals that the unsigned sum did not fit. The stored low bits are still well defined by that instruction. Signed two’s-complement values instead range from minus two to the b minus one through two to the b minus one minus one. The same bits therefore answer two different range questions.

For ordinary x86 ADD, the carry flag reports unsigned carry and the overflow flag reports signed overflow. Adding two positive signed operands that produces a negative signed result sets overflow; adding operands of opposite signs cannot overflow a fixed-width signed addition. These are instruction-specific effects, not universal properties of every arithmetic instruction. For example, some instructions preserve a flag that another updates. Read the entry for the exact operation and width before reusing a condition code in assembly.

r=(a+b) mod 2w,CF=⌊a+b2w⌋r=(a+b)\bmod 2^w,\qquad {\rm CF}=\left\lfloor\frac{a+b}{2^w}\right\rfloor
a and b are unsigned w-bit operand values and w is the bit width. For addition of two such operands, the unsigned carry CF is zero or one. All values are dimensionless; this is not a signed-overflow formula.↔ Scroll wide equations horizontally.

Signed overflow and unsigned carry are independent

Use sign bits to diagnose signed overflow: if both input sign bits agree and the result sign bit differs, the mathematical signed sum is outside the representable range. A carry out of the most significant position is not a substitute. In eight bits, 127+1 has no unsigned carry but does have signed overflow. Conversely, 255+1 has unsigned carry while its signed interpretation, minus one plus one, is exactly zero. Some sums have both conditions, and many have neither.

RV32I integer ADD does not set an x86-style condition-code register and does not trap on integer overflow; the low XLEN bits are retained. Software can detect conditions with suitable comparisons when necessary. Separately, a source language can give signed overflow a different status. In standard C, ordinary signed integer overflow is undefined behavior, while unsigned arithmetic is reduced modulo the type’s range after applicable conversions. A compiler need not preserve an intuitive signed-wraparound test unless the chosen language mode or operation actually supplies that guarantee.

OF=¬(sa⊕sb)∧(sa⊕sr){\rm OF}=\neg(s_a\oplus s_b)\land(s_a\oplus s_r)
s_a, s_b, and s_r are input and result sign bits for the same fixed-width two’s-complement addition. The Boolean expression defines signed overflow; it is dimensionless and does not describe floating-point exceptions.↔ Scroll wide equations horizontally.

Choose the desired arithmetic explicitly

A checked counter, a modular hash, and a saturating image operation require different behavior at their limits. A checked operation rejects or reports an out-of-range answer; modular arithmetic wraps; saturating arithmetic clamps to an endpoint. Replacing one with another changes the program, even if ordinary test inputs never approach the boundary. For allocation sizes, validate the multiplication and addition before passing the size onward. A wrapped size can allocate less storage than the later loop assumes it has.

The binomial lab has exact probabilities controlled by p and n. Set p to zero or one and predict the zero variance; then move away from the boundary. Its arithmetic is mathematical probability arithmetic, not a fixed-width ALU. The useful connection is the need to test boundary cases, not a claim that probability bars display overflow flags. For actual flags, construct a table of representative input patterns and compare the documented instruction behavior with the arithmetic interpretation you intend.

Two additions with different flags

For x86 eight-bit ADD, compare 127+1 with 255+1. Ignore other flags and interpret inputs both unsigned and signed.

  1. 127+1 gives mathematical unsigned sum 128, which fits in eight bits. The result pattern is 10000000, so CF=0. As signed arithmetic, positive 127 plus positive one should be 128, outside the signed range.

  2. The signed interpretation of 10000000 is minus 128, so the first addition has OF=1. For 255+1, the unsigned sum is 256; the low eight bits are zero and CF=1.

  3. Interpret the second operands as signed values: 255 represents minus one, and minus one plus one is zero. This fits, so OF=0. The result bits alone do not tell you which interpretation the application intended.

The result

The first addition has CF=0, OF=1; the second has CF=1, OF=0. A carry test cannot replace a signed-overflow test.

Explore the model

Interactive lab

Interactive lab · probability

Boundary cases in a specified model

Adjust binomial success probability p and trial count n. Predict the distribution and variance at p=0 and p=1. The lab is not an ALU and does not expose carry or overflow flags.

Boundary cases in a specified modelThese are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Current readings: Mean · np: 5; Variance · np(1−p): 2.5; Total probability: 1. All parameters are adjustable with the labeled controls below.03581000.070.150.220.3Successes kP(X = k)k = 0 · P = 9.77e-4
Exact probabilityHighlighted outcomeMean np
Mean · np
5
Variance · np(1−p)
2.5
Total probability
1
t = 0 s

These are exact binomial probabilities for independent trials with one fixed success probability. Playback tours the distribution; it does not resample outcomes. Parameter changes pause playback and recompute the model at the current elapsed time. Reset restores both parameters and returns time to zero. Playback starts paused. Adjust either parameter, then choose Play or Step.

Look forTesting boundaries matters in many models, but the meaning of each boundary is model-specific.

Check your understanding

An x86 eight-bit ADD computes 200+100. Which CF and OF values follow?

Try it yourself

For an eight-bit x86 ADD, compute 100+100 and interpret both flags. Would a standard C signed-char expression necessarily execute an eight-bit ADD?

Take a moment to make a prediction or work it out on paper. Open a hint when you need one.

Give me a hint

Keep the mathematical sum, stored pattern, and language conversions separate.

Compare with the solution

The low eight bits hold 200. No unsigned carry occurs, so CF=0; signed positive 100+100 exceeds 127 and produces a negative signed interpretation, so OF=1. C applies integer promotions to signed-char operands, so its source expression need not be an eight-bit machine addition; conversion back is a separate rule.

Take it with you

  • Carry answers an unsigned range question; overflow answers a signed one.
  • Read each architecture’s documented flag and trapping behavior.
  • Select checked, modular, or saturating arithmetic intentionally.

Read further

Original sources and deeper explanations to follow at your own pace.

Optional progress, saved in this browser when available. Revisit any lesson at any time.

Lesson not marked complete.