Quiz Entry - updated: 2026.07.07
What happens when integer multiplication overflows? Show an example.
The product wraps modulo 2^w; once it exceeds the type's maximum it can flip negative — e.g. repeatedly doubling 1,000,000 eventually goes negative in a 32-bit int.
* Repeatedly doubling a 32-bit signed int climbs to just under INT_MAX and then silently wraps to a negative value at iteration 12. *
int i = 1000000;
for (k = 0; k < 20; k++) {
printf("%2d: %d\n", k, i);
i = i * 2;
}
Output:
0: 1000000
1: 2000000
...
10: 1024000000
11: 2048000000
12: -198967296 <- OVERFLOW! Should be 4096000000
13: -397934592
...
Why this happens:
- 32-bit signed int max: ~2.1 billion
- When multiplying exceeds this, bits "wrap around"
- The sign bit gets set, making the result negative!
Security implication: Integer overflow is a common vulnerability class.
Go deeper:
Integer overflow (Wikipedia) — why results wrap modulo 2^W past the top of the range, with real-world failures (Ariane 5).
Modular arithmetic (Wikipedia) — the ring Z/2^W Z that formalizes exactly what wraparound computes.