Skip to content

Commit dd6bb51

Browse files
author
Stephen Gutekanst
committed
please stop messaging me about how C has bitfields too
Signed-off-by: Stephen Gutekanst <[email protected]>
1 parent 7c0bd5d commit dd6bb51

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

content/2022/packed-structs-in-zig.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,3 +147,28 @@ pub const ColorWriteMaskFlags = packed struct(u32) {
147147
Be sure to join the new [Mach engine Discord server](https://discord.gg/XNG3NZgCqp) where we're building the future of Zig game development.
148148
<br><br>
149149
You can also [sponsor my work](https://github.com/sponsors/slimsag) if you like what I'm doing! :)
150+
151+
## "But C has had bitfields since forever!"
152+
153+
Shortly after posting this article I was inundated with comments proclaiming "But C has had bitfields since forever!"
154+
155+
First, I'd like to say I was not aware of C bitfields at the time of writing - I simply had not ever come across usage of them. Secondly, I'd like to question: if C has bitfields, then why do seemingly all modern C APIs not use? Why do they all expose integer types instead?
156+
157+
And then I found the answer in the TC3 C specification:
158+
159+
<img width="803" alt="image" src="https://user-images.githubusercontent.com/3173176/189488251-738931cc-820a-4cd8-84d4-7320e3d870e6.png">
160+
161+
As [this user writes](https://news.ycombinator.com/item?id=32648232):
162+
163+
> 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.
164+
>
165+
> 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. However, it's still technically not portable.
166+
167+
My intention behind this article wasn't to say C is bad; but rather to say that I find Zig's packed structs quite nice. I actually come from a background mostly in Go - which absolutely does not have bitfields, packed structs, or arbitrary bit-width integers. Having never come across them in C either, my claims against C bitfields today could be summarized as:
168+
169+
* C's bitfields are more implementation-defined than Zig's.
170+
* C's bitfields being so implementation-defined, tend not to be used in modern APIs - so the fact that Zig has come up with a variant which _is used in practice in most APIs_ is very important.
171+
172+
In any case, I am not an expert in C bitfields! I just hate masking to check if bits are set, and the [insane number of ways](https://news.ycombinator.com/item?id=32646998) that exact same logic can be written - both correctly and incorrectly. We deserve nicer syntax to check if a bit field is set out of the box, Zig provides that and I am happier for it.
173+
174+
Please stop messaging me about how C has bitfields :)

0 commit comments

Comments
 (0)