Transitive bit-packing of fields

Nordlöw via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Sun Apr 30 04:02:52 PDT 2017


Have anybody found a way to do transitive packing of bitfields?

For instance, in


import std.bitmanip : bitfields;

struct X
{
     // one bit too many to fit in one byte
     mixin(bitfields!(bool, `a`, 1,
                      bool, `b`, 1,
                      ubyte, `c`, 7,
                      ubyte, `_pad`, 7);
}

struct Y
{
     // one unused bit
     mixin(bitfields!(ubyte, `d`, 7,
                      ubyte, `_pad`, 1);
}

struct XY
{
     X x;
     Y y;
}


`XY` will currently occupy 4 bytes, when only 1+1+7+7=16 bits are 
actually used in `a`, `b`, `c` and `d`.

Rust just got support for this.


More information about the Digitalmars-d-learn mailing list