You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Be sure to join the new [Mach engine Discord server](https://discord.gg/XNG3NZgCqp) where we're building the future of Zig game development.
148
148
<br><br>
149
149
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:
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