0Pricing
Zig Academy · Lesson

Arbitrary-Width Integers like u3

Use exact bit-width number types.

Arbitrary-Width Integers like u3 is a free Zig Academy lesson on CoddyKit — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Zig Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Beyond u8 and u32

Zig is not limited to the usual sizes. You can ask for an integer of any bit width, from u1 all the way up to enormous values.

The uN Pattern

Write u followed by a number to get an unsigned integer of exactly that many bits. So u3 holds three bits.

const level: u3 = 5;

Signed Versions Too

Swap the u for an i to get a signed integer of any width, so i4 covers a small signed range.

const delta: i4 = -3;

Exact Range Control

A u3 stores values from 0 to 7, because three bits give eight combinations. The type itself enforces that range.

Find the Limits

Ask the type for its bounds with maxInt and minInt, instead of memorizing them yourself.

const hi = std.math.maxInt(u3); // 7
const lo = std.math.minInt(u3); // 0

Even One-Bit Integers

A u1 holds just 0 or 1, acting like a single raw bit you can store and compute with directly.

const bit: u1 = 1;

u0 Is a Real Type

Even u0 exists. It can only ever be the value 0 and takes up no bits, which is handy in generic code.

Perfect for Packed Fields

Odd widths like u3 and u5 are ideal inside a packed struct, letting each field claim exactly the bits it needs.

const Color = packed struct {
    r: u5,
    g: u6,
    b: u5,
};

Watch for Overflow

Assigning 8 to a u3 overflows its range. In safe modes Zig catches this at runtime instead of letting it wrap silently.

Cast to Widen

Use @as or @intCast to move a narrow value into a wider type when you need more room to compute.

const small: u3 = 5;
const big: u16 = @as(u16, small);

Express Intent

Arbitrary widths let the type system document your real constraints, so a u4 makes it clear a value never exceeds 15. 🎯

Quick Check

Think about how many distinct values fit in three bits.

Recap

You met arbitrary-width integers like u3 and i4: exact ranges, perfect for packed fields, with overflow checked in safe modes. 🎉

Frequently asked questions

Is the “Arbitrary-Width Integers like u3” lesson free?

Yes — the full text of “Arbitrary-Width Integers like u3” is free to read here on the web, and the Zig Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Zig Academy course, upgrade to CoddyKit PRO.

What will I learn in “Arbitrary-Width Integers like u3”?

Use exact bit-width number types. You practise Zig Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Zig Academy?

No prior experience is required. Zig Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Arbitrary-Width Integers like u3” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Zig Academy lesson?

Yes. Every Zig Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. packed struct for Exact Layout
  2. Arbitrary-Width Integers like u3
  3. Bit Fields and Flags
  4. extern struct and ABI Layout
← Back to Zig Academy