Bitfields

monkyyy crazymonkyyy at gmail.com
Tue Nov 10 01:14:37 UTC 2026


On Monday, 9 November 2026 at 23:58:19 UTC, Anonymous wrote:
> Alright, I'll look into metaprogramming then. I'm aware of 
> std.bitmanip.bitfields, though I don't like that you have to 
> supply names in quotes. Any adivce before I go down the rabbit 
> hole?

i used this prompt

```prompt
install ldc
heavily bitpacked sumtype, fields, assignable

```d
struct foo{
   bool @1 flag1;
   ...
   ubyte @3 i;
}
struct bar{
   bool @1 flag1;
   ...
   float @3 f;
}
alias foobar=bitpacked!(foo,bar);
```

the "type" field, i.e. foo v bar is N^2 bits long
`hasi` is true for foo; they must be the same bits
use `__trait` directly
dlang has `foo=3` calls `foo(int)`; use this for the assign
```

it did some stupid things so I followed up with:
```prompt
add a toString
use a ubyte[sizeof] and bitshift, fuck your 64 bit requiriment
the @(1) was the sizeof it bits
```

... then 3 more... I thought this would one shot, oh well

I wrote this unit test manually and it seems correct

```d
struct circle{
   @(1) bool  filled;
   @(4) float x;
   @(4) float y;
   @(8) float d;
}
struct rect{
   @(1) bool filled;
   @(4) float x;
   @(4) float y;
   @(4) float w;
   @(4) float h;
}
unittest{
     import std;
     bitpacked!(circle,rect) foo;
     foo=rect(true,3,4,5,6);
     foo.writeln;
     foo=circle(true);
     foo.writeln;
     struct nil{}
     bitpacked!(nil,circle,rect) bar;
     bar.writeln;
     if( ! bar.hasfilled){
         bar=circle(true,3);
     }
     bar.writeln;
}
```


https://claude.ai/public/artifacts/8622cf12-8a3b-4f43-a519-ce74a3090769


More information about the Digitalmars-d-learn mailing list