TExcelTAD
Home › Zig › Zig Integers

Zig Integers

Integers are whole numbers. Zig gives you precise control over how many bits each integer uses and whether it can be negative — and it checks at runtime (in safe build modes) that arithmetic never silently overflows.

Integer types

The type name is i (signed) or u (unsigned) followed by the number of bits:

Type Range Typical use
u8 0 … 255 bytes, characters
i8 -128 … 127 small signed values
u16 / i16 up to 65 535 / ±32 767 ports, audio samples
u32 / i32 about 4.3 billion / ±2.1 billion counters, general numbers
u64 / i64 very large file sizes, timestamps
usize / isize pointer-sized (64 bits on most machines) lengths and indexes

Zig also supports arbitrary bit widths: u1, u3, i7, u24 and so on, up to 65 535 bits. They are handy for packed data formats.

zig · try it yourself
const std = @import("std");

pub fn main() void {
    std.debug.print("u8:  {d} .. {d}\n", .{ std.math.minInt(u8), std.math.maxInt(u8) });
    std.debug.print("i16: {d} .. {d}\n", .{ std.math.minInt(i16), std.math.maxInt(i16) });
    std.debug.print("u3:  {d} .. {d}\n", .{ std.math.minInt(u3), std.math.maxInt(u3) });
    std.debug.print("usize is {d} bytes\n", .{@sizeOf(usize)});
}
u8:  0 .. 255
i16: -32768 .. 32767
u3:  0 .. 7
usize is 8 bytes

Integer literals

zig · try it yourself
const std = @import("std");

pub fn main() void {
    const decimal = 1_000_000; // underscores improve readability
    const hex = 0xff;
    const octal = 0o755;
    const binary = 0b1010;
    const letter = 'A'; // a character literal is just a number

    std.debug.print("{d} {d} {d} {d} {d}\n", .{ decimal, hex, octal, binary, letter });
}
1000000 255 493 10 65

A literal on its own has the type comptime_int: an integer of unlimited size that exists only during compilation. It turns into a concrete type such as u8 the moment you store it somewhere typed. If it does not fit, you get a compile error:

zig
const std = @import("std");

pub fn main() void {
    const small: u8 = 300;
    std.debug.print("{d}\n", .{small});
}
text
./example.zig:4:23: error: type 'u8' cannot represent integer value '300'
    const small: u8 = 300;
                      ^~~

Overflow is detected

What happens when arithmetic goes past the end of the range at runtime? In C, unsigned values silently wrap around and signed overflow is undefined behaviour. In Zig's Debug and ReleaseSafe modes the program stops with a panic:

zig
var level: u8 = 250;
level += 10; // panic: integer overflow
text
thread 1 panic: integer overflow

If you want wrapping or clamping, say so explicitly with special operators (see the Operators lesson): +% wraps around and +| saturates at the maximum.

zig · try it yourself
const std = @import("std");

pub fn main() void {
    const level: u8 = 250;
    std.debug.print("wrapping:   {d}\n", .{level +% 10});
    std.debug.print("saturating: {d}\n", .{level +| 10});
}
wrapping:   4
saturating: 255

Division

Integer division with / is only allowed directly when both numbers are unsigned or the compiler knows they are positive. For signed integers that might be negative, Zig makes you choose how to round:

Builtin Rounds -7 by 2
@divTrunc(a, b) toward zero (like C) -3
@divFloor(a, b) toward minus infinity -4
@rem(a, b) remainder, sign of a -1
@mod(a, b) modulus, never negative for positive b 1
zig · try it yourself
const std = @import("std");

pub fn main() void {
    const a: i32 = -7;
    const b: i32 = 2;
    std.debug.print("trunc={d} floor={d}\n", .{ @divTrunc(a, b), @divFloor(a, b) });
    std.debug.print("rem={d} mod={d}\n", .{ @rem(a, b), @mod(a, b) });

    const pages: u32 = 17;
    std.debug.print("unsigned: {d} / 5 = {d} rest {d}\n", .{ pages, pages / 5, pages % 5 });
}
trunc=-3 floor=-4
rem=-1 mod=1
unsigned: 17 / 5 = 3 rest 2

Dividing by zero is a compile error when the compiler can see it, and a panic at runtime otherwise.

Tip: Use usize for anything that counts or indexes memory (lengths, positions). Array .len is a usize, so matching it avoids conversions.

Test yourself

What is the largest value a u8 can hold?

  1. 127
  2. 255
  3. 256
  4. 65535

In a Debug build, what happens when x += 1 overflows a u8?

  1. It wraps to 0
  2. It stays at 255
  3. The program panics with 'integer overflow'
  4. Undefined behaviour

What does @mod(-7, 3) return?

  1. -1
  2. 1
  3. 2
  4. -2