Hacker News

Favorites Setup
Comment by viega | original | C for Rust programmers
[−]viega · 2026-10-10 Sat 13:51 UTC · link
A bool does automatically cast to an unsized bit slice.

Meaning, if you have a struct and you want to bit-pack your booleans, you can declare each one as, say, uint8_t some_bool : 1;

You may then do `x.some_bool = true;` etc.

It's a small nicety to avoid bitwise operators, anyway.

[−]bjackman · 2026-10-11 Sun 09:33 UTC · link
I do tend to think people under use bitfields.

I suspect this is because they are usually introduced like "here are bitfields, but DO NOT USE THEM for anything that might become ABI because their order is implementation defined". I think that causes people to mentally file it under <sketchy language features to avoid> when actually there are many contexts where the ABI risk is not real.