Packed Structs
By default, all struct fields in Zig are naturally aligned to that of
@alignOf(FieldType)
(the
ABI size), but without a defined layout. Sometimes you may want to have struct
fields with a defined layout that do not conform to your C ABI. packed
structs
allow you to have extremely precise control of your struct fields, allowing you
to place your fields on a bit-by-bit basis.
Inside packed structs, Zig’s integers take their bit-width in space (i.e. a
u12
has an @bitSizeOf
of 12, meaning it will take up 12 bits in the packed struct). Bools also take up
1 bit, meaning you can implement bit flags easily.