Hacker News new | past | comments | ask | show | jobs | submit login
Packed structs in Zig make bit/flag sets trivial (hexops.com)
6 points by fanf2 6 days ago | hide | past | favorite | 2 comments





>The in-memory representation of bit fields is implementation-defined. Therefore, if you’re calling into an external API that takes a uint32_t like in the example without an explicit remapping, you may or may not like the results.

>In practice, everything you’re likely to come across will be little endian nowadays, and the ABI you’re using will most likely order your struct from top to bottom in memory, so they will look the same most of the time.

Unfortunately it is not that simple:

https://github.com/mahkoh/repr-c/blob/0c218ac5a6f82034e649fe...


> Secondly, I’d like to question: if C has bitfields, then why do seemingly all modern C APIs not use?

Because an unsigned integer containing many flags is convenient for setting/toggling/clearing multiple options at once. At least in C doing that with bit fields is a pain.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: