Integer overflow occurs when a calculation produces a result too large for the allocated bit width. The excess bits are lost, and the stored value wraps around to an incorrect number. Overflow can cause programs to produce wrong results, crash, or — in safety-critical systems — fail catastrophically.
Why is there a maximum value for a binary number?
Every integer in a computer is stored using a fixed number of bits. The number of bits limits how many different values can be represented. For an n-bit unsigned integer:
- Minimum value: 0 (all bits are 0)
- Maximum value: 2ⁿ − 1 (all bits are 1)
| Bit width | Maximum unsigned value | Example maximum |
|---|---|---|
| 4 bits | 2⁴ − 1 = 15 | 1111 |
| 8 bits | 2⁸ − 1 = 255 | 11111111 |
| 16 bits | 2¹⁶ − 1 = 65,535 | |
| 32 bits | 2³² − 1 ≈ 4.3 billion |
When you add 1 to the maximum value, there is no valid binary representation within those bits — overflow occurs.
What happens when an 8-bit register overflows?
Worked example: 255 + 1 in an 8-bit register
| Binary | Denary | |
|---|---|---|
| First number | 1111 1111 |
255 |
| Add | 0000 0001 |
1 |
| Mathematical result | 1 0000 0000 |
256 |
| Stored in 8 bits | 0000 0000 |
0 |
The 9th bit (the carry bit) cannot fit in the 8-bit register. It is discarded, and the stored result is 00000000 = 0 — completely wrong. The value has wrapped around from the maximum to the minimum.
This is like a car's odometer rolling over from 999,999 km back to 000,000 — the display resets, but the true distance is not lost; the physical engine has not gone back to zero. In software, however, the program genuinely uses the wrapped value, not the correct one.
How does overflow affect signed (two's complement) numbers?
For signed integers using two's complement, the range for an 8-bit number is −128 to +127. Adding 1 to +127 should give +128, but:
| Binary | Two's complement value | |
|---|---|---|
| +127 | 0111 1111 |
127 |
| Add 1 | — | — |
| Result | 1000 0000 |
−128 |
The carry causes the sign bit (leftmost bit) to flip from 0 to 1, making a large positive number appear as a large negative number. This is a signed overflow.
What are the real-world consequences of overflow?
Overflow has caused serious real-world incidents:
- Ariane 5 rocket (1996): a 64-bit floating-point velocity value was converted into a 16-bit integer. The value exceeded the 16-bit maximum, causing an overflow, an unhandled exception, and the self-destruction of the rocket — a £370 million failure from a single type conversion.
- Year 2000 (Y2K) bug: while not integer overflow in the strictest sense, it illustrates the same principle — dates stored in two digits "wrapped" at 99 → 00, threatening to represent 2000 as 1900.
- Game score bugs: old video games occasionally wrapped player scores or enemy counters back to zero when they exceeded the maximum stored value — famously exploited in Pac-Man's 256th level, which corrupted due to a tile counter overflow.
How do programming languages handle overflow?
| Language / approach | Overflow behaviour |
|---|---|
| C, C++ (unsigned) | Wraps around (defined behaviour) |
| C, C++ (signed) | Undefined behaviour — results are unpredictable |
| Python | No overflow — integers grow to arbitrary precision automatically |
| Java | Wraps around silently for int; exceptions possible with Math.addExact() |
| Rust | Panics in debug mode; wraps in release mode (configurable) |
| Hardware flags | Most CPUs set a carry flag or overflow flag that software can check |
Python avoids overflow by automatically promoting integers to arbitrary precision, using as much memory as needed. This makes Python safer for beginners but slower for numerical code.
How does overflow relate to binary shifts?
Overflow also occurs during binary shifts: if you shift a value left and a 1-bit is pushed off the left edge of the register, that bit is lost permanently. For example, shifting 11000000 (192 in an 8-bit register) left by one place gives 10000000 (128) — the leading 1 was pushed out and the result is wrong. This is why programs must check whether a shift will overflow before performing it on large values.
Frequently asked questions
Does Python ever overflow?
Python integers never overflow because Python dynamically allocates more memory as integers grow. 2 ** 10000 is a perfectly valid computation in Python — it just produces an enormous number. This is different from most low-level languages where integers are fixed-width. In Python, overflow can still occur indirectly in NumPy arrays (which use fixed-width types for performance) or when interfacing with hardware or other languages.
How do programmers prevent overflow?
The main strategies are: (1) choose a large enough integer type for the expected range of values — use 64-bit instead of 32-bit if values might exceed four billion; (2) check the result before using it, using overflow-safe functions where the language provides them; (3) use arbitrary-precision libraries when exact results for very large numbers are required; (4) use type annotations and static analysis tools that can warn about potential overflow at compile time. In safety-critical systems, formal verification may be used to prove overflow cannot occur.
What is the difference between overflow and underflow?
Integer overflow occurs when a result is too large for the bit width. Integer underflow occurs when a result is too small (too negative). For unsigned integers, subtracting 1 from 0 underflows: 0000 0000 − 1 wraps to 1111 1111 (255 in 8 bits). Floating-point underflow is a different concept — it occurs when a floating-point number becomes too close to zero to be represented (smaller than the minimum positive value), causing it to round to zero.
Why is overflow relevant to cybersecurity?
Buffer overflow is a specific type of overflow used in many historical security exploits. When a program writes data into a fixed-size buffer without checking the length, extra data overflows into adjacent memory — potentially overwriting the return address of a function with an attacker's chosen value. When the function returns, control jumps to the attacker's code instead of the correct location. This class of vulnerability is now mitigated by modern OS features (address space layout randomisation, non-executable stacks) but remains relevant in embedded systems and legacy software.
Professor Turing can walk you through overflow, binary arithmetic, and all GCSE data representation topics with Socratic questions at aitutors.me.