www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Union with bits ?

reply Paul <phshaffer gmail.com> writes:
I would like to have labeled bits in a union with a ubyte.  
Something like this:
```d
struct MyStruct {
     union {
         ubyte status;
         bit A, B, C…etc
     }
}
```
Is something like this possible?

Thanks
Jun 13 2023
next sibling parent reply Rene Zwanenburg <renezwanenburg gmail.com> writes:
On Wednesday, 14 June 2023 at 00:59:30 UTC, Paul wrote:
 I would like to have labeled bits in a union with a ubyte.  
 Something like this:
 ```d
 struct MyStruct {
     union {
         ubyte status;
         bit A, B, C…etc
     }
 }
 ```
 Is something like this possible?

 Thanks
You can do something like this if you don't mind compiling with -preview=bitfields: ```d union { ubyte status; struct { bool A : 1, B : 1, C : 1, D : 1; } } ```
Jun 14 2023
parent Adam D Ruppe <destructionator gmail.com> writes:
On Wednesday, 14 June 2023 at 08:51:19 UTC, Rene Zwanenburg wrote:
 You can do something like this if you don't mind compiling with 
 -preview=bitfields:
That doesn't do what you think it does. There's no guarantee the bits will actually line up with the status byte. The best way to do what the OP wants is to add property getters and setters to the struct with the ubyte that masks the member.
Jun 14 2023
prev sibling parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 6/13/23 17:59, Paul wrote:
 I would like to have labeled bits in a union with a ubyte. Something
 like this:
 ```d
 struct MyStruct {
      union {
          ubyte status;
          bit A, B, C…etc
      }
 }
 ```
 Is something like this possible?

 Thanks
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
Jun 14 2023
parent reply Paul <phshaffer gmail.com> writes:
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?
Jun 14 2023
parent reply =?UTF-8?Q?Ali_=c3=87ehreli?= <acehreli yahoo.com> writes:
On 6/14/23 15:04, Paul wrote:

 Question: Why do you say "may be slower than necessary"?  Do you mean
 compile or runtime or both?
Definitely at compile time because the string that gets mixed-in first needs to be generated from your specification. For that, the bitfields() function must be compiled and executed at compile-time. Then the mixed-in code will be compiled. I remember reading here about lackluster compilation performance in that area. And it may be slow at run time as well depending on certain checks the functions perform. For example, you will notice that setting the value 2 will fail at run time on a 1-bit member. If you would never pass a 2 value for such a function, than it may be seen as extra cost. By the way, the string that bitfields() generates can be visualized by simply printing it: const code = bitfields!( ubyte, "A", 1, ubyte, "B", 1, ubyte, "C", 1, ubyte, "padding", 5,); writeln(code); The output is not pretty for the human eye but those are basically the getters and setters that Adam D Ruppe mentioned. Ali
Jun 14 2023
parent Paul <phshaffer gmail.com> writes:
On Wednesday, 14 June 2023 at 22:44:41 UTC, Ali Çehreli wrote:
 By the way, the string that bitfields() generates can be 
 visualized by simply printing it:

     const code =
         bitfields!(
             ubyte, "A", 1,
             ubyte, "B", 1,
             ubyte, "C", 1,
             ubyte, "padding", 5,);
     writeln(code);

 The output is not pretty for the human eye but those are 
 basically the getters and setters that Adam D Ruppe mentioned.

 Ali
Excellent help. Thanks Adam and Ali.
Jun 14 2023