Check whether an integer, or an addition, subtraction or multiplication result, overflows a signed or unsigned 8-, 16-, 32- or 64-bit type.
Related Calculators
- BCD / Binary Coded Decimal Calculator
- Greater Than and Less Than Calculator
- Floating Point Normalization Calculator
- Log Base 2 Calculator
- Exponent Calculator
- All Math and Numbers Calculators
Overflow Error Formula
The following formula measures only the amount by which an addition exceeds an upper bound. It is not the wrapped result and does not detect values below a minimum. Compare the exact result with both bounds for a complete range check.
Variables:
- E is the amount above the maximum, or zero if the sum does not exceed it
- CV is the current value
- I is the increment value
- MV is the maximum value that can be held without overflow
For an n-bit unsigned integer, the range is 0 to 2โฟ โ 1. For signed twoโs-complement, it is โ2โฟโปยน to 2โฟโปยน โ 1. Calculate exactly, then compare with both bounds. Illustrative wrapping takes the result modulo 2โฟ; for signed display, subtract 2โฟ when the normalized value is at least 2โฟโปยน.
What is an Overflow Error?
Integer overflow occurs when an exact arithmetic result lies outside the selected integer typeโs representable range, either above its maximum or below its minimum. A programming language may wrap, report an error or specify other behavior. This calculator shows a mathematical range check and an illustrative modulo result; it does not simulate a particular languageโs operand promotion or checked-arithmetic rules.
How to Calculate Overflow Error?
The following steps outline how to calculate the Overflow Error.
- First, choose the integer type and bit width to determine its minimum and maximum (MV).
- Next, determine the current value (CV) before the increment.
- Next, determine the increment value (I) that will be added to the current value.
- Next, for this addition example, compute E = max(0, (CV + I) โ MV).
- Finally, E greater than zero means the sum exceeds the maximum. Also compare the exact sum with the minimum; E alone does not detect a result below that bound.
- After inserting the variables and calculating the result, check your answer with the calculator above.
Example Problem :
Use the following variables as an example problem to test your knowledge.
maximum value (MV) = 255
current value (CV) = 250
increment value (I) = 10
The exact sum is 250 + 10 = 260. For an 8-bit unsigned type, the maximum is 255, so the amount above maximum is 5. The illustrative modular result is 260 mod 256 = 4. These are different quantities.
