Packed structs in Zig make bit/flag sets trivial
devlog.hexops.com
Packed structs in Zig make bit/flag sets trivial
1–10 of 147 posts
Re: Packed structs in Zig make bit/flag sets trivial
#2Re: Packed structs in Zig make bit/flag sets trivial
#3This is a nice syntax, but the functionality seems essentially the same as bit fields in C. Does this provide anything additional?
Re: Packed structs in Zig make bit/flag sets trivial
#4This is a nice syntax, but the functionality seems essentially the same as bit fields in C. Does this provide anything additional?
Re: Packed structs in Zig make bit/flag sets trivial
#5This is a nice syntax, but the functionality seems essentially the same as bit fields in C. Does this provide anything additional?
D just went ahead and implemented bit fields. They work, and it's hard to find any way to improve on them.
Re: Packed structs in Zig make bit/flag sets trivial
#6This is a nice syntax, but the functionality seems essentially the same as bit fields in C. Does this provide anything additional?
One major difference appears to be that C bitfields memory layout is compiler-dependant. The other major difference is Zig's arbitrary-bit-width integer types just leading to less footguns I would speculate
Re: Packed structs in Zig make bit/flag sets trivial
#7Doesn't this give you if alpha OR blue is set?
Errors like this are another reason syntactical sugar for readability is important.
Re: Packed structs in Zig make bit/flag sets trivial
#8This is a nice syntax, but the functionality seems essentially the same as bit fields in C. Does this provide anything additional?
Admittedly I didn't know C had bit fields like that, so I didn't cover them. I've never seen them used in practice and I've used quite a lot of C libraries. I wonder why that is? One major difference appears to be that C bitfields memory layout is compiler-dependant. The other major difference is Zig's arbitrary-bit-width integer types just leading to less footguns I would speculate
Re: Packed structs in Zig make bit/flag sets trivial
#9if (mask & (WGPUColorWriteMask_Alpha|WGPUColorWriteMask_Blue)) { // alpha and blue are set.. } Doesn't this give you if alpha OR blue is set? Errors like this are another reason syntactical sugar for readability is important.
Re: Packed structs in Zig make bit/flag sets trivial
#10This is a nice syntax, but the functionality seems essentially the same as bit fields in C. Does this provide anything additional?
Just one thing that Zig improves: in C if you need the bit offsets of the fields you'll still need lots of defines with the offsets/masks and such. In Zig it can be extracted from the packed struct at comptime