Zig panics with integer overflow on a u8 counter: how do I get wrapping or saturating arithmetic?

There are two more families. Saturating operators clamp at the limits: +| on a u8 gives 250 + 10 = 255, useful for things like a PWM duty value or a signal level where wrapping would be wrong.

For values coming from outside, where overflow is a real error to handle, std.math.add(u8, a, b) returns error.Overflow instead of panicking, and the builtin @addWithOverflow gives you the wrapped result together with an overflow bit. The builtin’s exact form has changed between releases, so check the language reference for your compiler version.

Read more here: Source link