std.bitmanip

Jump to: bitfields FloatRep DoubleRep BitArray opIndex opIndexAssign dup opApply reverse sort opEquals opCmp init opCast opCom opAnd opOr opXor opSub opAndAssign opOrAssign opXorAssign opSubAssign opCatAssign opCat opCat_r

Bit-level manipulation facilities.

Authors:
Walter Bright, Andrei Alexandrescu


template bitfields(T...)
Allows creating bit fields inside structs and classes.

Example:

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;
The example above creates 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.

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.

Example:

struct A
{
mixin(bitfields!(
bool, "flag1", 1,
bool, "flag2", 1,
uint, "", 6));
}
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.


struct FloatRep;
Allows manipulating the fraction, exponent, and sign parts of a float separately. The definition is:


struct FloatRep
{
union
{
float value;
mixin(bitfields!(
uint, "fraction", 23,
ubyte, "exponent", 8,
bool, "sign", 1));
}
}


struct DoubleRep;
Allows manipulating the fraction, exponent, and sign parts of a double separately. The definition is:


struct DoubleRep
{
union
{
double value;
mixin(bitfields!(
ulong, "fraction", 52,
ushort, "exponent", 11,
bool, "sign", 1));
}
}


struct BitArray;
An array of bits.


const bool opIndex(size_t i);
bool opIndexAssign(bool b, size_t i);
Support for [index] operation for BitArray.


BitArray dup();
Support for array.dup property for BitArray.


int opApply(int delegate(ref bool) dg);
int opApply(int delegate(ref uint, ref bool) dg);
Support for foreach loops for BitArray.


BitArray reverse();
Support for array.reverse property for BitArray.


BitArray sort();
Support for array.sort property for BitArray.


int opEquals(BitArray a2);
Support for operators == and != for bit arrays.


int opCmp(BitArray a2);
Implement comparison operators.


void init(bool[] ba);
Set BitArray to contents of ba[]


void init(void[] v, size_t numbits);
Map BitArray onto v[], with numbits being the number of bits in the array. Does not copy the data.

This is the inverse of opCast.


void[] opCast();
Convert to void[].


BitArray opCom();
Support for unary operator ~ for bit arrays.


BitArray opAnd(BitArray e2);
Support for binary operator & for bit arrays.


BitArray opOr(BitArray e2);
Support for binary operator | for bit arrays.


BitArray opXor(BitArray e2);
Support for binary operator ^ for bit arrays.


BitArray opSub(BitArray e2);
Support for binary operator - for bit arrays.

a - b for BitArrays means the same thing as a & ~b.


BitArray opAndAssign(BitArray e2);
Support for operator &= bit arrays.


BitArray opOrAssign(BitArray e2);
Support for operator |= for bit arrays.


BitArray opXorAssign(BitArray e2);
Support for operator ^= for bit arrays.


BitArray opSubAssign(BitArray e2);
Support for operator -= for bit arrays.

a -= b for BitArrays means the same thing as a &= ~b.


BitArray opCatAssign(bool b);
BitArray opCatAssign(BitArray b);
Support for operator ~= for bit arrays.


BitArray opCat(bool b);
BitArray opCat_r(bool b);
BitArray opCat(BitArray b);
Support for binary operator ~ for bit arrays.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值