Integer overflow happens when an arithmetic result exceeds the destination type’s range. In Zig, ordinary addition asserts that overflow will not occur; in safe mode, violating that assertion is illegal behavior and can trigger a debug-mode panic. section 2.1.3
Choose an arithmetic operation according to what the program should do when a result exceeds its range:
- Use +% (or -% and *%) to wrap the result instead of panicking. section 6.5.4
- Use @addWithOverflow or @subWithOverflow to receive the wrapped result and a u1 flag indicating whether overflow occurred; @mulWithOverflow provides the same approach for multiplication. section 6.5.4
A Fibonacci value too large for i32
A recursive Fibonacci function using ordinary + eventually overflows an i32; n = 47 is given as a test case. Zig can panic and show a stack trace pointing to the addition. Replacing + with +% makes the result wrap instead. section 2.1.3
Table 6.3 More register instructions
| Mnemonic | Description |
|
Add a constant to a register, wrapping on overflow. |
|
Set register |
|
Set register |
|
Set register |
|
Set register |
|
Set register |
|
Set register |
The CHIP-8 register instructions show where the choice matters: some additions wrap, while others store an overflow bit in register 0. section 6.5.4

