Live data from Hacker News

Packed structs in Zig make bit/flag sets trivial

devlog.hexops.com

1–10 of 147 posts

Re: Packed structs in Zig make bit/flag sets trivial

#3

This 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

#5

This 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.

It also specified the layout (little endian) and disallowed reordering / padding / etc.

Re: Packed structs in Zig make bit/flag sets trivial

#6

This 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

#8
post #6

This 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

I don't know why you haven't come across them but personally I've used bitfields a lot over my career as a video game programmer at different companies. You can have arbitrary bit width integers as well. It's not really very different to zig.

Re: Packed structs in Zig make bit/flag sets trivial

#9
post #7

if (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.

Indeed, you would need to check that the mask ends up being exactly alpha and blue, otherwise it's just an automatic boolean conversion from integer to boolean, which many languages are doing away with due to all the bugs it produces.

Re: Packed structs in Zig make bit/flag sets trivial

#10

This is a nice syntax, but the functionality seems essentially the same as bit fields in C. Does this provide anything additional?

C bitfields are problematic and often aren't used in places where you'd think they'd be useful. Zig seems to be doing its best to get bitfields right.

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

Post reply on HN