Bitfields
Dennis
dkorpel at gmail.com
Tue Nov 10 15:08:12 UTC 2026
On Monday, 9 November 2026 at 23:58:19 UTC, Anonymous wrote:
> Any adivce before I go down the rabbit hole?
I would consider using a basic integer for the bit packing, and
adding getter methods to access it as the 2 different types like
the union fields would.
```D
struct Node {
ubyte Type_storage: 5;
bool Is_Frob: 1;
byte Frob_Direction: 2;
int Regular_Field;
/* more regular fields */
Node_Type Type() const => *cast(Node_Type*) &Type_storage;
Node_Type_Id Type_Id() const => cast(Node_Type_Id) Type_storage;
}
```
Because of D's property syntax you can call `Node.Type` or
`Node.Type_Id` without parens just like the union field access,
and in optimized builds the generated code will be just as fast.
More information about the Digitalmars-d-learn
mailing list