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
Calculator Functions
| Function | What it does | What it requires |
|---|---|---|
| Does a value overflow? | Checks whether a whole number fits in a chosen integer type | Requires the integer type, the bit width and the integer value |
| Does a sum or product overflow? | Checks whether adding, subtracting or multiplying two whole numbers gives a result that fits | Requires the integer type, the bit width, the two integers and the operation |
The integer type is signed (twoโs complement) or unsigned, and the bit width is 8, 16, 32 or 64. Every result lists the exact result, the allowed range, the wrapped result and the smallest bit width that would hold the number. It also shows how far the number is past the limit, or how much room is left.
Overflow Error Formulas
| x | Exact result, worked out without any size limit |
| n | Bit width of the integer type |
This is the range of a signed integer. An unsigned integer holds 0 to 2โฟ โ 1 [1]. A result outside the range is an overflow: above the maximum or below the minimum.
| Bit width | Signed range | Unsigned range |
|---|---|---|
| 8 | โ128 to 127 | 0 to 255 |
| 16 | โ32,768 to 32,767 | 0 to 65,535 |
| 32 | โ2,147,483,648 to 2,147,483,647 | 0 to 4,294,967,295 |
| 64 | โ2โถยณ to 2โถยณ โ 1 | 0 to 2โถโด โ 1 |
The signed ranges match the Java types byte, short, int and long [2].
Does a value overflow?
The value is compared with both ends of the range. 2,147,483,648 in a signed 32-bit integer is above the maximum of 2,147,483,647 by 1. The amount above is x minus the maximum, and the amount below is the minimum minus x.
Does a sum or product overflow?
The sum, difference or product is worked out exactly first, then compared with the range. 250 + 10 = 260 in an unsigned 8-bit integer is above the maximum of 255 by 5.
Wrapped result
The wrapped result is what is left when only the lowest n bits are kept. For a signed type, 2โฟ is subtracted when w is 2โฟโปยน or more. 260 in 8 bits wraps to 260 โ 256 = 4. Java does this on overflow in addition and multiplication [3], and gives no signal that it happened [2].
Limits of This Estimate
- The wrapped result is one possible outcome, not what every language does. Rust stops the program on overflow in debug builds and wraps in release builds [1]. Other languages have their own rules.
- The two numbers entered are not checked against the type, only the result is. Languages may also widen small types before the arithmetic, which this check does not model.
- This checks fixed-width integers as used in programming. It does not cover floating-point numbers or the overflow message on a handheld calculator.
Sources
- The Rust Programming Language, Chapter 3.2, Data Types: signed and unsigned ranges, and overflow behavior in Rust
- The Java Language Specification, Java SE 21, Chapter 4, Sections 4.2.1 and 4.2.2: ranges of byte, short, int and long, and no overflow signal
- The Java Language Specification, Java SE 21, Chapter 15, Sections 15.17.1 and 15.18.2: overflow keeps the low-order bits
