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
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