Overflow: + vs +% vs +|
Trapping, wrapping, and saturating math.
Numbers Have Limits
Every sized integer has a maximum it can hold. Push past it and you hit overflow, the moment a value tries to grow beyond its range.
Zig Catches Overflow
By default, the plain + operator traps overflow. In a safe build, going past the limit stops your program instead of corrupting the value.
var x: u8 = 255;
x = x + 1; // panics: overflowAll lessons in this course
- Arithmetic and Comparison Operators
- Overflow: + vs +% vs +|
- Bitwise Operators
- Boolean Logic and Short-Circuiting