I would like to have labeled bits in a union with a ubyte. Something like this:
struct MyStruct {
union {
ubyte status;
bit A, B, C…etc
}
}
Is something like this possible?
Thanks
Thread overview | |||||||||
---|---|---|---|---|---|---|---|---|---|
|
June 14, 2023 Union with bits ? | ||||
---|---|---|---|---|
| ||||
I would like to have labeled bits in a union with a ubyte. Something like this:
Is something like this possible? Thanks |
June 14, 2023 Re: Union with bits ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | 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:
Is something like this possible? Thanks You can do something like this if you don't mind compiling with -preview=bitfields:
|
June 14, 2023 Re: Union with bits ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Rene Zwanenburg | 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.
|
June 14, 2023 Re: Union with bits ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | 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 |
June 14, 2023 Re: Union with bits ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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?
|
June 14, 2023 Re: Union with bits ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Paul | 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 |
June 15, 2023 Re: Union with bits ? | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | 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.
|