Answer#
Add 1 to 2147483647, the largest signed 32-bit integer. Each language was run on exactly that.
| Language | Result | Why |
|---|---|---|
| Python 3.13.7 | 2147483648 | Integers have no fixed size; masking with 0xFFFFFFFF is needed to get 32-bit behaviour |
| Node.js v22.19.0 | 2147483648; -2147483648 after a bitwise-or with 0 | Numbers are doubles; a bitwise operator forces a 32-bit integer |
| Java 17 | -2147483648; Math.addExact throws integer overflow | int wraps; the exact methods check |
| C# (.NET 10.0.302) | -2147483648 in unchecked; OverflowException in checked | The default context here was unchecked |
| Go 1.26.5 | -2147483648 | Signed integers wrap |
| Rust 1.93.0 | debug build: panic; release build: -2147483648 | Overflow checks follow the build profile |
| C | undefined behaviour for signed int | Not run here; this is what the C standard says |
The Rust program shows all four of Rust's explicit choices, and the plain + last because it ends the program in a debug build. It uses black_box so that the compiler cannot compute the answer ahead of time:
fn main() {
let max = std::hint::black_box(i32::MAX);
println!("wrapping_add : {}", max.wrapping_add(1));
println!("checked_add : {:?}", max.checked_add(1));
println!("saturating_add: {}", max.saturating_add(1));
println!("plain + : {}", max + 1);
}(release build)
wrapping_add : -2147483648
checked_add : None
saturating_add: 2147483647
plain + : -2147483648
(debug build)
wrapping_add : -2147483648
checked_add : None
saturating_add: 2147483647
thread 'main' panicked at src\bin\ovf2.rs:6:36:
attempt to add with overflowThe other languages, as run. Java:
int x = Integer.MAX_VALUE;
System.out.println(x + 1);
try { Math.addExact(x, 1); } catch (ArithmeticException e) { System.out.println(e.getMessage()); }-2147483648
integer overflowC#:
int x = int.MaxValue;
Console.WriteLine(unchecked(x + 1));
try { Console.WriteLine(checked(x + 1)); } catch (OverflowException e) { Console.WriteLine(e.Message); }-2147483648
Arithmetic operation resulted in an overflow.JavaScript printed 2147483648, then -2147483648 for (2147483647 + 1) | 0, and 9007199254740992 for 9007199254740992 + 1 (the doubles can no longer count by one). Python printed 2147483648 both for the plain sum and for the sum masked with 0xFFFFFFFF: the number is still below 2 to the power 32, so the mask leaves it alone, and Python never wraps by itself.
How to do it#
- For each integer in the old code, find out what it relied on: wrap-around (hashes, checksums, random number generators), growth (Python big integers), or "never overflows" in practice.
- For intended wrap-around, use
wrapping_add,wrapping_muland friends in Rust, or theWrapping<T>type, so that the code means the same in debug and release. - For arithmetic that must not overflow, use
checked_*and handleNone, or widen the type (i64,u128). - For code ported from Python, choose a width from the data, or use a big-integer crate if values really have no bound (no crate was run here).
- Run the tests in both debug and release builds (
cargo testandcargo test --release) so that both overflow behaviours are exercised.
Common mistakes#
- Porting a hash function that relied on wrap-around with plain
+and*. It panics in a debug build. - Using
asto narrow a wider result and calling it a check (see the thread on C integers). - Believing that Rust always panics on overflow. The release default is to wrap.
Related questions#
Can I make release builds panic on overflow too?#
Yes, with overflow-checks = true in [profile.release]; it costs some speed. Measure before deciding.
Which type does a literal like 2147483647 have?#
Whatever the context needs; i32 if nothing else decides it.
Sources#
- The Rust Reference, "Integer overflow": https://doc.rust-lang.org/reference/expressions/operator-expr.html#overflow
i32::checked_add: https://doc.rust-lang.org/std/primitive.i32.html#method.checked_add