Get the latest tech news

Packed structs in Zig make bit/flag sets trivial


As we've been building Mach engine, we've been using a neat little pattern in Zig that enables writing flag sets more nicely in Zig than in other languages. Here's a brief explainer.

The Zig compiler will take care of running the comptime code block here for us when building, and it will verify that the byte size of@This()(the type we’re inside of, the ColorWriteMaskFlags struct in this case) matches the@sizeOf(u32). Be sure to join the new Mach engine Discord server where we’re building the future of Zig game development. I just hate masking to check if bits are set, and the insane number of ways that exact same logic can be written - both correctly and incorrectly.

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Packed

Packed

Photo of Bit

Bit

Photo of zig

zig

Related news:

News photo

OpenAI's latest o1 model family tries to emulate 'reasoning' – tho might overthink things a bit

News photo

Linear, symmetric, self-selecting 14-bit molecular memristors (2023)

News photo

Show HN: HypergraphZ – A Hypergraph Implementation in Zig