Integers
Integer Literals
const decimal_int = 98222;
const hex_int = 0xff;
const another_hex_int = 0xFF;
const octal_int = 0o755;
const binary_int = 0b11110000;
Runtime Integer Values
Integer literals have no size limitation, and if any undefined behavior occurs, the compiler catches it.
However, once an integer value is no longer known at compile-time, it must have a known size, and is vulnerable to undefined behavior.
fn divide(a: i32, b: i32) i32 {
return a / b;
}
In this function, values a
and b
are known only at runtime, and thus this division operation is vulnerable to both Integer Overflow and Division by Zero.
Operators such as +
and -
cause undefined behavior on integer overflow. Also available are operations such as +%
and -%
which are defined to have wrapping arithmetic on all targets.
Zig supports arbitrary bit-width integers, referenced by using an identifier of i
or u followed by digits. For example, the identifier i7
refers to a signed 7-bit integer. The maximum allowed bit-width of an integer type is 65535
.
See also: