Union with bits ?

Paul phshaffer at gmail.com
Wed Jun 14 22:04:03 UTC 2023


On Wednesday, 14 June 2023 at 14:43:58 UTC, Ali Çehreli wrote:
> D's string mixin syntax may not be the best, the implementation 
> may be slower than necessary, and the concept may be strange 
> (not macros but very similar) but I still find phobos's 
> bifields to be brilliant.
>
>   https://dlang.org/phobos/std_bitmanip.html#bitfields
>
> import std.bitmanip;
>
> struct MyStruct {
>     union {
>         ubyte status;
>         mixin(bitfields!(
>                   ubyte, "A", 1,
>                   ubyte, "B", 1,
>                   ubyte, "C", 1,
>                   ubyte, "padding", 5,));
>     }
> }
>
> void main() {
>     auto m = MyStruct();
>     m.B = 1;
>     assert(m.status == 2);
> }
>
> Ali

Thanks Ali!  I believe this is what I'm looking for.  I searched 
our website and library but somehow missed this.

Question: Why do you say "may be slower than necessary"?  Do you 
mean compile or runtime or both?


More information about the Digitalmars-d-learn mailing list