Check whether an integer, or an addition, subtraction or multiplication result, overflows a signed or unsigned 8-, 16-, 32- or 64-bit type.

Whole number; commas and spaces are ignored.

Optional settings


Related Calculators

Calculator Functions

FunctionWhat it doesWhat it requires
Does a value overflow?Checks whether a whole number fits in a chosen integer typeRequires 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 fitsRequires 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

โˆ’2โฟโปยน โ‰ค x โ‰ค 2โฟโปยน โˆ’ 1 [1]
xExact result, worked out without any size limit
nBit 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 widthSigned rangeUnsigned range
8โˆ’128 to 1270 to 255
16โˆ’32,768 to 32,7670 to 65,535
32โˆ’2,147,483,648 to 2,147,483,6470 to 4,294,967,295
64โˆ’2โถยณ to 2โถยณ โˆ’ 10 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

w = x mod 2โฟ

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

  1. The Rust Programming Language, Chapter 3.2, Data Types: signed and unsigned ranges, and overflow behavior in Rust
  2. 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
  3. The Java Language Specification, Java SE 21, Chapter 15, Sections 15.17.1 and 15.18.2: overflow keeps the low-order bits