0Pricing
Zig Academy · Lesson

Bit Fields and Flags

Pack booleans and small fields tightly.

Many Flags, One Byte

Settings are often just on-or-off switches. Packing them as bit fields stores many flags in a single tiny integer.

Booleans Take One Bit

Inside a packed struct, a bool occupies exactly one bit. Stack several together and they share a single byte.

const Mode = packed struct {
    bold: bool,
    italic: bool,
    underline: bool,
};

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