typestar

Integer widths in Zig

The width is part of the type: u8 and i32, u64 — and u3 if three bits is what you mean.

const std = @import("std");

pub fn main() void {
    // Widths are part of the type: u8, i32, u64 -- even u3 exists.
    const crew: u8 = 12;
    const depth: i32 = -180;
    const grains: u64 = 4_294_967_296;
    const die_faces: u3 = 6;

    std.debug.print("crew {d}, depth {d} m\n", .{ crew, depth });
    std.debug.print("grains {d}, die faces {d}\n", .{ grains, die_faces });
    std.debug.print("a u3 tops out at {d}\n", .{std.math.maxInt(u3)});
}

How it works

  1. u8, i32, and u64 size the crew, the depth, and the sand grains exactly.
  2. u3 is a real type holding 0 through 7 — arbitrary widths are legal.
  3. std.math.maxInt(u3) asks the type itself for its ceiling.

Keywords and builtins used here

The run, in numbers

Lines
13
Characters to type
423
Tokens
111
Three-star pace
70 tpm

At the three-star pace of 70 tokens a minute, this run takes about 95 seconds.

Type this snippet

Step 3 of 3 in Values, step 3 of 27 in Language basics.

← Previous Next →