DIP: add Bit Fields
Nick Treleaven
nick at geany.org
Sat Mar 9 15:02:47 UTC 2024
On Thursday, 7 March 2024 at 04:26:56 UTC, Walter Bright wrote:
> https://github.com/WalterBright/documents/blob/master/bitfields.md
> One cannot take a reference to or the address of a bitfield
Taking a ref is not detected yet:
https://issues.dlang.org/show_bug.cgi?id=24263
I was wondering how to read/write the bits of a bitfield set as
their underlying type. So I tried putting a bitfield set inside
an anonymous union:
```d
import std.stdio;
struct S
{
union
{
uint u;
uint a:1,
b:1,
c:1;
}
}
static assert(S.sizeof == 4);
void main()
{
S s;
s.tupleof.writeln(); // 0000
s.u++;
s.tupleof.writeln(); // 1111
}
```
I was a bit surprised that setting `u` to 1 sets all 3 bitfields
to 1 as well - I was assuming the union had 2 uint members where
the 2nd one didn't have a name. Then I realized I needed to put
the bitfields inside an anonymous struct, because otherwise each
one was a union member. Anyway doing that does work.
More information about the dip.development
mailing list