View source code
Display the source code in std/bitmanip.d from which this page was generated on github.
Report a bug
If you spot a problem with this page, click here to create a Bugzilla issue.
Improve this page
Quickly fork, edit online, and submit a pull request for this page. Requires a signed-in GitHub account. This works well for small changes. If you'd like to make larger changes you may want to consider using local clone.

Enum member std.bitmanip.bitfields

Allows creating bit fields inside structs and classes.

enum bitfields(T...) = createStorageAndFields!T.result;

The type of a bit field can be any integral type or enumerated type. The most efficient type to store in bitfields is bool, followed by unsigned types, followed by signed types.

See Also

BitFlags

Example

Create a bitfield pack of eight bits, which fit in one ubyte. The bitfields are allocated starting from the least significant bit, i.e. x occupies the two least significant bits of the bitfields storage.

struct A
{
    int a;
    mixin(bitfields!(
        uint, "x",    2,
        int,  "y",    3,
        uint, "z",    2,
        bool, "flag", 1));
}

A obj;
obj.x = 2;
obj.z = obj.x;

writeln(obj.x); // 2
writeln(obj.y); // 0
writeln(obj.z); // 2
writeln(obj.flag); // false

Example

The sum of all bit lengths in one bitfield instantiation must be exactly 8, 16, 32, or 64. If padding is needed, just allocate one bitfield with an empty name.

struct A
{
    mixin(bitfields!(
        bool, "flag1",    1,
        bool, "flag2",    1,
        uint, "",         6));
}

A a;
writeln(a.flag1); // 0
a.flag1 = 1;
writeln(a.flag1); // 1
a.flag1 = 0;
writeln(a.flag1); // 0

Example

enums can be used too

enum ABC { A, B, C }
struct EnumTest
{
    mixin(bitfields!(
              ABC, "x", 2,
              bool, "y", 1,
              ubyte, "z", 5));
}

Authors

Walter Bright, Andrei Alexandrescu, Jonathan M Davis, Alex Rønne Petersen, Damian Ziemba, Amaury SECHET

License

Boost License 1.0.