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.
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.
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 \).
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.
The Core Rule
int maxes out around \( 2.1 \times 10^9 \). A long long maxes out around \( 9.2 \times 10^{18} \).int.long long when intermediate or final values might exceed \( \approx 2 \times 10^9 \).C++ Integer Type Limits
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
long long instead of int.(long long)a * b.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:
Fix: Use long long for the result and cast before multiplying.
Example 2: Summation Overflow
You need to sum \( n \) values, each up to \( 10^9 \), where \( n \) can be up to \( 200{,}000 \).
Fix:
Example 3: Intermediate Overflow
Even when the final answer fits in an int, the intermediate value might not:
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.
1 is int (32 bits). Shift overflows. Undefined behavior.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:
Type Selection Flowchart
Common Mistakes
(long long)(a * b) casts after the multiply. The overflow already happened inside the parentheses.int, but an intermediate product or sum may not. Always estimate the largest intermediate value.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++?
int uses one bit for the sign, leaving 31 bits for the value: \( 2^{31} - 1 = 2{,}147{,}483{,}647 \approx 2.1 \times 10^{9} \). Option (a) is the unsigned 32-bit max, and (b) is the long long max.Question 2 of 5
Which line of code correctly prevents overflow when computing \( a \times b \), where both a and b are int?
a to long long before the multiplication, so the product is computed in 64-bit. Options (a) and (d) compute a * b as int first (overflow happens), then widen the already-wrong result.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?
int (\( \approx 2.1 \times 10^{9} \)) and unsigned int (\( \approx 4.3 \times 10^{9} \)). long long can hold up to \( \approx 9.2 \times 10^{18} \), which is more than enough.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?
long long. Without it, 1 is an int, and shifting an int by 40 is undefined behavior (exceeds 31 usable bits). With 1LL, the shift operates safely in 64-bit space.