| ▲ | thechao 5 hours ago | |||||||
I know this is a bit cursed; but, I always wanted a bitfield-on-steroids construct: | ||||||||
| ▲ | a_t48 an hour ago | parent | next [-] | |||||||
It is a bit cursed, but you can do this in C/C++. 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 2 hours ago | parent | prev | next [-] | |||||||
You might want to have a look at the unboxing and packing annotations that are proposed for Virgil. The unboxing mechanism is implemented and there was a prototype of the packing mechanism implemented by Bradley for his thesis. I am working on making a more robust implementation that I can land. 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. | ||||||||
| ▲ | Lvl999Noob 4 hours ago | parent | prev | next [-] | |||||||
Are you saying you want foo and bar to completely overlap? And baz and foo / bar to partially overlap? And have lots of unused bits in there too? | ||||||||
| ||||||||
| ▲ | sestep 4 hours ago | parent | prev | next [-] | |||||||
I think you can do this with Virgil, but I'm having trouble finding the exact doc page at the moment: https://github.com/titzer/virgil | ||||||||
| ||||||||
| ▲ | _bohm 5 hours ago | parent | prev | next [-] | |||||||
You can kinda do this with Zig’s packed structs and arbitrary-width integers | ||||||||
| ||||||||
| ▲ | bsder 2 hours ago | parent | prev [-] | |||||||
Look at Erlang bit syntax: https://www.erlang.org/doc/system/bit_syntax.html It can even be used for pattern matching. I don't know whether Gleam or Elixir inherited it. | ||||||||