Bit Fields and Flags
Pack booleans and small fields tightly.
Bit Fields and Flags is a free Zig Academy lesson on CoddyKit — lesson 3 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.
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,
};Mix Bools and Small Ints
A packed struct can blend flags with narrow integers, modeling a real register where each bit group means something.
const Reg = packed struct {
enabled: bool,
speed: u3,
mode: u2,
};Read a Flag by Name
You access each bit through its field name, so the code reads clearly even though the data is tightly packed.
var m = Mode{ .bold = true, .italic = false, .underline = false };
if (m.bold) {}Flip a Flag
Set or clear a flag by assigning to the field. No bit masking by hand, Zig handles the bit math for you.
m.italic = true;The Raw Bits View
Need the underlying number? @bitCast turns the flag struct into its backing integer for storage or comparison.
const bits: u3 = @bitCast(m);Decode Incoming Bits
Reading bits from a file or device? Cast the raw integer back into your struct to decode it into named flags.
const incoming: u3 = 0b101;
const m: Mode = @bitCast(incoming);Enums as Bit Fields
A field can be an enum with a small backing integer, giving named choices that still fit in just a few bits.
const Speed = enum(u2) { slow, medium, fast };Why Bother Packing
Bit fields save memory and match hardware exactly, which matters in embedded code, drivers, and binary file formats.
Layout Stays Predictable
Because the struct is packed, the bit order never shifts. What you declare is what hardware and other programs will see.
Clear Code, Tight Data
The big win: you write readable field names while the data stays as compact as hand-rolled bit twiddling. 🎛️
Quick Check
Think about how much space a bool uses inside a packed struct.
Recap
You packed flags into one integer: bools take one bit, mix in narrow ints and enums, and @bitCast moves between struct and raw bits. 🎉
Frequently asked questions
Is the “Bit Fields and Flags” lesson free?
Yes — the full text of “Bit Fields and Flags” 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 “Bit Fields and Flags”?
Pack booleans and small fields tightly. 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 3 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Bit Fields and Flags” 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.