digitalmars.D.learn - Union with bits ?
- Paul (12/12) Jun 13 2023 I would like to have labeled bits in a union with a ubyte.
- Rene Zwanenburg (11/23) Jun 14 2023 You can do something like this if you don't mind compiling with
- Adam D Ruppe (5/7) Jun 14 2023 That doesn't do what you think it does. There's no guarantee the
- =?UTF-8?Q?Ali_=c3=87ehreli?= (22/34) Jun 14 2023 D's string mixin syntax may not be the best, the implementation may be
- Paul (5/27) Jun 14 2023 Thanks Ali! I believe this is what I'm looking for. I searched
- =?UTF-8?Q?Ali_=c3=87ehreli?= (22/24) Jun 14 2023 Definitely at compile time because the string that gets mixed-in first
- Paul (2/14) Jun 14 2023 Excellent help. Thanks Adam and Ali.
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
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? ThanksYou 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
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
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? ThanksD'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
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); } AliThanks 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
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
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. AliExcellent help. Thanks Adam and Ali.
Jun 14 2023