01 / 21
\(2^{31}\)
OVERFLOW

Avoiding Integer
Overflow

Learn how integers are stored, why they have fixed limits, and how to choose the right type to keep your C++ programs safe from silent, dangerous bugs.

C++

What is an Integer in C++?

In C++, an integer is a whole number (no fractions or decimals) stored in a fixed amount of memory. The keyword int tells the compiler to reserve a block of bits to hold your number.

Fixed Size
Every integer type uses a predetermined number of bits: 8, 16, 32, or 64.
Finite Range
A fixed number of bits means a fixed range of values: there is always a maximum and a minimum.

Bits Determine the Range

With \( n \) bits, an unsigned integer can store values from \( 0 \) to \( 2^n - 1 \). A signed integer (the default in C++) uses one bit for the sign, giving a range of \( -2^{n-1} \) to \( 2^{n-1} - 1 \).

Signed 32-bit int range -2,147,483,648
\((-2^{31})\)
0 2,147,483,647
\((2^{31} - 1)\)
Signed 64-bit long long range
\(-9.2 \times 10^{18}\)
\((-2^{63})\)
0
\(9.2 \times 10^{18}\)
\((2^{63} - 1)\)

What is Integer Overflow?

Integer overflow occurs when a calculation produces a value outside the range of its type. The extra bits are silently discarded, and the stored result is wrong: often wrapping around to a completely different number.

MAX MIN (wrap!) 0 Like an odometer rolling past its maximum

The Core Rule

1
Know your type's limits. An int maxes out around \( 2.1 \times 10^9 \). A long long maxes out around \( 9.2 \times 10^{18} \).
2
Estimate the result. If multiplying two values each up to \( 10^5 \), the product can reach \( 10^{10} \): that exceeds int.
3
Choose the right type. Use long long when intermediate or final values might exceed \( \approx 2 \times 10^9 \).

C++ Integer Type Limits

int8_t 127 int16_t 32,767 int 2,147,483,647
\((\approx 2.1 \times 10^{9})\)
long long
9,223,372,036,854,775,807 \((\approx 9.2 \times 10^{18})\)
Unsigned variants double the positive range (but no negatives):
unsigned int
\(\approx 4.3 \times 10^{9}\)
unsigned long long
\(\approx 1.8 \times 10^{19}\)

Signed vs Unsigned

Signed Unsigned
Stores negatives? Yes No
32-bit max \( 2^{31} - 1 \approx 2.1 \times 10^9 \) \( 2^{32} - 1 \approx 4.3 \times 10^9 \)
Overflow behavior Undefined behavior Wraps (modulo \( 2^n \))
Default in C++ Yes (int) Only with unsigned keyword
Use when General purpose, negative values needed Bit manipulation, indices in special cases

How to Prevent Overflow

1
Read the constraints. Identify the maximum possible value for each variable.
2
Estimate the result. Multiply the largest inputs together. Will the product, sum, or intermediate value exceed \( 2.1 \times 10^9 \)?
3
Choose the type. If the answer is yes, declare variables as long long instead of int.
4
Cast before computing. In expressions mixing types, cast to the wider type before the operation: (long long)a * b.
5
Use constants wisely. Write 1LL instead of 1 in expressions like 1LL << 40.

Example 1: Multiplication Overflow

You need to compute the area of a rectangle whose sides can be up to \( 100{,}000 \). Using int:

int width = 100000; int height = 100000; int area = width * height; // Overflow! // 100000 × 100000 = 10,000,000,000 // int max ≈ 2,147,483,647 → OVERFLOW

Fix: Use long long for the result and cast before multiplying.

long long area = (long long)width * height; // ✓ Safe

Example 2: Summation Overflow

You need to sum \( n \) values, each up to \( 10^9 \), where \( n \) can be up to \( 200{,}000 \).

int sum = 0; // Wrong type! for (int i = 0; i < n; i++) sum += a[i];

Fix:

long long sum = 0; // ✓ Can hold up to ~9.2 × 10¹⁸ for (int i = 0; i < n; i++) sum += a[i];

Example 3: Intermediate Overflow

Even when the final answer fits in an int, the intermediate value might not:

// Compute (a * b) / c where a=1000000, b=1000000, c=500000 int a = 1000000, b = 1000000, c = 500000; int result = (a * b) / c; // a*b overflows first!
a * b
\(= 10^{12}\)
OVERFLOW
int max: \(\approx 2.1 \times 10^{9}\)
/ c Wrong answer Fix: (long long)a * b / c = 2,000,000 ✓

The 1LL Trick

Integer literals like 1 or 1000 are int by default. If you write 1 << 35, the shift is done on an int: undefined behavior on most systems.

int bad = 1 << 35; // UB: shifting int beyond 31 bits long long good = 1LL << 35; // ✓ = 34,359,738,368
1 << 35
Literal 1 is int (32 bits). Shift overflows. Undefined behavior.
1LL << 35
Suffix LL makes it long long (64 bits). Plenty of room.

Using <climits> Constants

C++ provides named constants for every type's limits. Use them instead of memorizing magic numbers:

#include <climits> int mx = INT_MAX; // 2,147,483,647 int mn = INT_MIN; // -2,147,483,648 long long llmx = LLONG_MAX; // 9,223,372,036,854,775,807

Type Selection Flowchart

Estimate max value
Value < \(2.1 \times 10^{9}\) ?
Yes Use int No
Value < \(9.2 \times 10^{18}\) ?
Yes Use long long No Use __int128 or big-int library Remember: check intermediates too, not just the final answer.

Common Mistakes

✕ Casting too late
Writing (long long)(a * b) casts after the multiply. The overflow already happened inside the parentheses.
✕ Only checking the final result
The final answer may fit in int, but an intermediate product or sum may not. Always estimate the largest intermediate value.
✕ Mixing signed and unsigned
When you subtract unsigned values and the result is negative, it wraps to a huge positive number. Use signed types unless you have a specific reason.

Quick Reference Table

Type Bits Max Value Use When
int 32 \( \approx 2.1 \times 10^{9} \) Values < 2 billion
unsigned int 32 \( \approx 4.3 \times 10^{9} \) Non-negative only
long long 64 \( \approx 9.2 \times 10^{18} \) Large products/sums
unsigned long long 64 \( \approx 1.8 \times 10^{19} \) Very large non-negative

Question 1 of 5

What is the approximate maximum value of a signed 32-bit int in C++?

Question 2 of 5

Which line of code correctly prevents overflow when computing \( a \times b \), where both a and b are int?

Question 3 of 5

You must sum \( 200{,}000 \) values, each up to \( 10^{9} \). The sum could reach \( 2 \times 10^{14} \). Which type should you use for the accumulator?

Question 4 of 5

What happens when a signed int overflows in C++?

Question 5 of 5

What does the suffix LL do in the expression 1LL << 40?