(no title)
thechao | 1 month ago
struct Dang : bits 64 // 64 bits wide, int total
{
foo : bits 5 @ 0; // 5 bits wide at bit offset 0
bar : bits 5 @ 0;
baz : bits 16 @ 4; // 16 bits wide at bit offset 4
tom : bits 11 @ 32;
};
a_t48|1 month ago
https://godbolt.org/z/vPKEdnjan
The member types don't actually matter here so we can have a little fun and macro it without having to resort to templates to get "correct" types. Highly recommend not doing this in production code. If nothing else, there's no compiler protection against offset+size being > total size, but one could add it with a static assert! (I've done so in the godbolt link)Edit: if you're talking about Zig, sorry!
titzer|1 month ago
https://arxiv.org/abs/2410.11094
I'm not sure I understand your example; if I am looking at it right, it has overlapping bitfields.
But supposing you didn't want overlapping fields, you could write:
And the compiler would smash the bits together (highest order bits first).If you wanted more control, you can specify where every bit of every field goes using a bit pattern:
Where each of T, b, z, and r represent a bit of each respective field.thechao|1 month ago
Lvl999Noob|1 month ago
metaltyphoon|1 month ago
bsder|1 month ago
It can even be used for pattern matching.
I don't know whether Gleam or Elixir inherited it.
sestep|1 month ago
titzer|1 month ago
https://arxiv.org/abs/2410.11094
Bradley implemented a prototype of the packing solver, but it doesn't do the full generality of what is proposed in the paper.
zozbot234|1 month ago
_bohm|1 month ago
unknown|1 month ago
[deleted]