Integer Rules
Zig supports hex, octal and binary integer literals.
Underscores may also be placed between digits as a visual separator.
“Integer Widening” is allowed, which means that integers of a type may coerce to an integer of another type, providing that the new type can fit all of the values that the old type can.
If you have a value stored in an integer that cannot coerce to the type that you
want, @intCast
may be
used to explicitly convert from one type to the other. If the value given is out
of the range of the destination type, this is detectable illegal behaviour.
Integers, by default, are not allowed to overflow. Overflows are detectable illegal behaviour. Sometimes, being able to overflow integers in a well-defined manner is a wanted behaviour. For this use case, Zig provides overflow operators.
Normal Operator | Wrapping Operator |
---|---|
+ | +% |
- | -% |
* | *% |
+= | +%= |
-= | -%= |
*= | *%= |