| Category | Functions |
|---|---|
| Bit constructs | BitArraybitfieldsbitsSet |
| Endianness conversion | bigEndianToNativelittleEndianToNativenativeToBigEndiannativeToLittleEndianswapEndian |
| Integral ranges | appendpeekreadwrite |
| Floating-Point manipulation | DoubleRepFloatRep |
| Tagging | taggedClassReftaggedPointer |
Sourcestd/bitmanip.d
bitfields(T...)();bitfields insidestructs,classes andunions.bitfields isbool, followedby unsigned types, followed by signed types.Each non-bool entry of thebitfield will be represented by thenumber of bits specified by the user. The minimum and the maximumnumbers that represent this domain can be queried by using the nameof the variable followed by_min or_max.LimitationThe number of bits in abitfield is limited to 8, 16,32 or 64. If padding is needed, an entry should be explicitlyallocated with an empty name.
Implementation detailsBitfields are internally stored in anubyte,ushort,uint orulong depending on the number of bitsused. The bits are filled in the order given by the parameters,starting with the lowest significant bit. The name of the (private)variable used for saving thebitfield is created by concatenatingall of the variable names, each preceded by an underscore, anda suffix_bf.
| T | A list of template parameters divided into chunks of 3 items. Each chunk consists (in this order) of a type, a name and a number. Together they define an entry of thebitfield: a variable of the given type and name, which can hold as many bits as the number denotes. |
bitfields are allocated starting from theleast significant bit, i.e.x occupies the two least significant bitsof thebitfields 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);// 2writeln(obj.y);// 0writeln(obj.z);// 2writeln(obj.flag);// false
struct A{mixin(bitfields!(bool,"flag1", 1,bool,"flag2", 1,uint,"", 6));}A a;writeln(a.flag1);// 0a.flag1 = 1;writeln(a.flag1);// 1a.flag1 = 0;writeln(a.flag1);// 0
enum ABC { A, B, C }struct EnumTest{mixin(bitfields!( ABC,"x", 2,bool,"y", 1,ubyte,"z", 5));}
taggedPointer(T : T*, string name, Ts...);struct A{int a;mixin(taggedPointer!(uint*,"x",bool,"b1", 1,bool,"b2", 1));}A obj;obj.x =newuint;obj.b1 =true;obj.b2 =false;
taggedClassRef(T, string name, Ts...) if (is(T == class))struct A{int a;mixin(taggedClassRef!( Object,"o",uint,"i", 2));}A obj;obj.o =new Object();obj.i = 3;
FloatRep = FloatingPointRepresentation!float.FloatingPointRepresentation;structFloatRep{union {float value;mixin(bitfields!(uint,"fraction", 23,ubyte,"exponent", 8,bool,"sign", 1)); }enumuint bias = 127, fractionBits = 23, exponentBits = 8, signBits = 1;}
FloatRep rep = {value: 0};writeln(rep.fraction);// 0writeln(rep.exponent);// 0assert(!rep.sign);rep.value = 42;writeln(rep.fraction);// 2621440writeln(rep.exponent);// 132assert(!rep.sign);rep.value = 10;writeln(rep.fraction);// 2097152writeln(rep.exponent);// 130
FloatRep rep = {value: 1};writeln(rep.fraction);// 0writeln(rep.exponent);// 127assert(!rep.sign);rep.exponent = 126;writeln(rep.value);// 0.5rep.exponent = 130;writeln(rep.value);// 8
FloatRep rep = {value: 1};rep.value = -0.5;writeln(rep.fraction);// 0writeln(rep.exponent);// 126assert(rep.sign);rep.value = -1. / 3;writeln(rep.fraction);// 2796203writeln(rep.exponent);// 125assert(rep.sign);
DoubleRep = FloatingPointRepresentation!double.FloatingPointRepresentation;structDoubleRep{union {double value;mixin(bitfields!(ulong,"fraction", 52,ushort,"exponent", 11,bool,"sign", 1)); }enumuint bias = 1023, signBits = 1, fractionBits = 52, exponentBits = 11;}
DoubleRep rep = {value: 0};writeln(rep.fraction);// 0writeln(rep.exponent);// 0assert(!rep.sign);rep.value = 42;writeln(rep.fraction);// 1407374883553280writeln(rep.exponent);// 1028assert(!rep.sign);rep.value = 10;writeln(rep.fraction);// 1125899906842624writeln(rep.exponent);// 1026
DoubleRep rep = {value: 1};writeln(rep.fraction);// 0writeln(rep.exponent);// 1023assert(!rep.sign);rep.exponent = 1022;writeln(rep.value);// 0.5rep.exponent = 1026;writeln(rep.value);// 8
DoubleRep rep = {value: 1};rep.value = -0.5;writeln(rep.fraction);// 0writeln(rep.exponent);// 1022assert(rep.sign);rep.value = -1. / 3;writeln(rep.fraction);// 1501199875790165writeln(rep.exponent);// 1021assert(rep.sign);
DoubleRep x;x.value = 1.0;assert(x.fraction == 0 && x.exponent == 1023 && !x.sign);x.value = -0.5;assert(x.fraction == 0 && x.exponent == 1022 && x.sign);x.value = 0.5;assert(x.fraction == 0 && x.exponent == 1022 && !x.sign);
DoubleRep x;x.fraction = 1125899906842624;x.exponent = 1025;x.sign =true;writeln(x.value);// -5.0
BitArray;BitArray can be manipulated individuallyor by the standard bitwise operators&,|,^,~,>>,<< and also byother effective member functions; most of them work relative to theBitArray'sdimension (seedim), instead of itslength.import std.algorithm.comparison : equal;import std.range : iota;bool[] buf =newbool[64 * 3];buf[0 .. 64] =true;BitArray b =BitArray(buf);assert(b.bitsSet.equal(iota(0, 64)));b <<= 64;assert(b.bitsSet.equal(iota(64, 128)));
import std.algorithm.comparison : equal;auto b =BitArray([1, 0]);b ~=true;writeln(b[2]);// 1b ~=BitArray([0, 1]);auto c =BitArray([1, 0, 1, 0, 1]);writeln(b);// cassert(b.bitsSet.equal([0, 2, 4]));
import std.algorithm.comparison : equal;auto b =BitArray([1, 1, 0, 1]);b &=BitArray([0, 1, 1, 0]);assert(b.bitsSet.equal([1]));b.flip;assert(b.bitsSet.equal([0, 2, 3]));
import std.format : format;auto b =BitArray([1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]);writeln(format("%b", b));// "1_00001111_00001111"
import std.format : format;BitArray b;b =BitArray([]);writeln(format("%s", b));// "[]"assert(format("%b", b)isnull);b =BitArray([1]);writeln(format("%s", b));// "[1]"writeln(format("%b", b));// "1"b =BitArray([0, 0, 0, 0]);writeln(format("%b", b));// "0000"b =BitArray([0, 0, 0, 0, 1, 1, 1, 1]);writeln(format("%s", b));// "[0, 0, 0, 0, 1, 1, 1, 1]"writeln(format("%b", b));// "00001111"b =BitArray([0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]);writeln(format("%s", b));// "[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]"writeln(format("%b", b));// "00001111_00001111"b =BitArray([1, 0, 0, 0, 0, 1, 1, 1, 1]);writeln(format("%b", b));// "1_00001111"b =BitArray([1, 0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]);writeln(format("%b", b));// "1_00001111_00001111"
ba);bool[]ba | Source array ofbool values. |
import std.algorithm.comparison : equal;bool[] input = [true,false,false,true,true];auto a = BitArray(input);writeln(a.length);// 5assert(a.bitsSet.equal([0, 3, 4]));// This also works because an implicit cast to bool[] occurs for this array.auto b = BitArray([0, 0, 1]);writeln(b.length);// 3assert(b.bitsSet.equal([2]));
import std.algorithm.comparison : equal;import std.array : array;import std.range : iota, repeat;BitArray a =true.repeat(70).array;writeln(a.length);// 70assert(a.bitsSet.equal(iota(0, 70)));
v, size_tnumbits);size_t[] source = [1, 2, 3, 3424234, 724398, 230947, 389492];enum sbits = size_t.sizeof * 8;auto ba = BitArray(source, source.length * sbits);foreach (n; 0 .. source.length * sbits){auto nth_bit =cast(bool) (source[n / sbits] & (1L << (n % sbits)));assert(ba[n] == nth_bit);}
void[]v | Source array.v.length must be a multple ofsize_t.sizeof. |
size_tnumbits | Number of bits to be mapped from the source array, i.e. length of the createdBitArray. |
import std.algorithm.comparison : equal;auto a = BitArray([1, 0, 0, 1, 1]);// Inverse of the cast.autov =cast(void[]) a;auto b = BitArray(v, a.length);writeln(b.length);// 5assert(b.bitsSet.equal([0, 3, 4]));// a and b share the underlying data.a[0] = 0;writeln(b[0]);// 0writeln(a);// b
import std.algorithm.comparison : equal;size_t[] source = [0b1100, 0b0011];enum sbits = size_t.sizeof * 8;auto ba = BitArray(source, source.length * sbits);// The least significant bit in each unit is this unit's starting bit.assert(ba.bitsSet.equal([2, 3, sbits, sbits + 1]));
// Example from the doc for this constructor.staticimmutable size_t[] sourceData = [1, 0b101, 3, 3424234, 724398, 230947, 389492];size_t[] source = sourceData.dup;enum sbits = size_t.sizeof * 8;auto ba = BitArray(source, source.length * sbits);foreach (n; 0 .. source.length * sbits){auto nth_bit =cast(bool) (source[n / sbits] & (1L << (n % sbits))); writeln(ba[n]);// nth_bit}// Example of mapping only part of the array.import std.algorithm.comparison : equal;auto bc = BitArray(source, sbits + 1);assert(bc.bitsSet.equal([0, sbits]));// Source array has not been modified.writeln(source);// sourceData
dim() const;length() const;length(size_tnewlen);opIndex(size_ti) const;i'th bit in theBitArray.staticvoid fun(const BitArray arr){auto x = arr[0]; writeln(x);// 1}BitArray a;a.length = 3;a[0] = 1;fun(a);
opIndexAssign(boolb, size_ti);i'th bit in theBitArray.opSliceAssign(boolval);val.import std.algorithm.comparison : equal;auto b = BitArray([1, 0, 1, 0, 1, 1]);b[] =true;// all bits are setassert(b.bitsSet.equal([0, 1, 2, 3, 4, 5]));b[] =false;// none of the bits are setassert(b.bitsSet.empty);
opSliceAssign(boolval, size_tstart, size_tend);start and ends at indexend - 1 with the values specified byval.import std.algorithm.comparison : equal;import std.range : iota;import std.stdio;auto b = BitArray([1, 0, 0, 0, 1, 1, 0]);b[1 .. 3] =true;assert(b.bitsSet.equal([0, 1, 2, 4, 5]));bool[72] bitArray;auto b1 = BitArray(bitArray);b1[63 .. 67] =true;assert(b1.bitsSet.equal([63, 64, 65, 66]));b1[63 .. 67] =false;assert(b1.bitsSet.empty);b1[0 .. 64] =true;assert(b1.bitsSet.equal(iota(0, 64)));b1[0 .. 64] =false;assert(b1.bitsSet.empty);bool[256] bitArray2;auto b2 = BitArray(bitArray2);b2[3 .. 245] =true;assert(b2.bitsSet.equal(iota(3, 245)));b2[3 .. 245] =false;assert(b2.bitsSet.empty);
flip();import std.algorithm.comparison : equal;import std.range : iota;// positions 0, 2, 4 are setauto b = BitArray([1, 0, 1, 0, 1, 0]);b.flip();// after flipping, positions 1, 3, 5 are setassert(b.bitsSet.equal([1, 3, 5]));bool[270] bits;auto b1 = BitArray(bits);b1.flip();assert(b1.bitsSet.equal(iota(0, 270)));
flip(size_tpos);posauto ax = BitArray([1, 0, 0, 1]);ax.flip(0);writeln(ax[0]);// 0bool[200] y;y[90 .. 130] =true;auto ay = BitArray(y);ay.flip(100);writeln(ay[100]);// 0
count() const scope;auto a = BitArray([0, 1, 1, 0, 0, 1, 1]);writeln(a.count);// 4BitArray b;writeln(b.count);// 0bool[200] boolArray;boolArray[45 .. 130] =true;auto c = BitArray(boolArray);writeln(c.count);// 85
dup() const;BitArray a;BitArray b;a.length = 3;a[0] = 1; a[1] = 0; a[2] = 1;b = a.dup;writeln(b.length);// 3foreach (i; 0 .. 3) writeln(b[i]);// (((i ^ 1) & 1) ? true : false)
opApply(scope int delegate(ref bool)dg);opApply(scope int delegate(bool)dg) const;opApply(scope int delegate(size_t, ref bool)dg);opApply(scope int delegate(size_t, bool)dg) const;bool[] ba = [1,0,1];auto a = BitArray(ba);int i;foreach (b;a){switch (i) {case 0:assert(b ==true);break;case 1:assert(b ==false);break;case 2:assert(b ==true);break;default:assert(0); } i++;}foreach (j,b;a){switch (j) {case 0:assert(b ==true);break;case 1:assert(b ==false);break;case 2:assert(b ==true);break;default:assert(0); }}
reverse() return;BitArray b;bool[5] data = [1,0,1,1,0];b = BitArray(data);b.reverse;foreach (i; 0 .. data.length) writeln(b[i]);// data[4 - i]
sort() return;size_t x = 0b1100011000;auto ba = BitArray(10, &x);ba.sort;foreach (i; 0 .. 6) writeln(ba[i]);// falseforeach (i; 6 .. 10) writeln(ba[i]);// true
opEquals(ref const BitArraya2) const;bool[] ba = [1,0,1,0,1];bool[] bb = [1,0,1];bool[] bc = [1,0,1,0,1,0,1];bool[] bd = [1,0,1,1,1];bool[] be = [1,0,1,0,1];bool[] bf = [1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0];bool[] bg = [1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1];auto a = BitArray(ba);auto b = BitArray(bb);auto c = BitArray(bc);auto d = BitArray(bd);auto e = BitArray(be);auto f = BitArray(bf);auto g = BitArray(bg);assert(a != b);assert(a != c);assert(a != d);writeln(a);// eassert(f != g);
opCmp(BitArraya2) const;bool[] ba = [1,0,1,0,1];bool[] bb = [1,0,1];bool[] bc = [1,0,1,0,1,0,1];bool[] bd = [1,0,1,1,1];bool[] be = [1,0,1,0,1];bool[] bf = [1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1];bool[] bg = [1,0,1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,1,0];auto a = BitArray(ba);auto b = BitArray(bb);auto c = BitArray(bc);auto d = BitArray(bd);auto e = BitArray(be);auto f = BitArray(bf);auto g = BitArray(bg);assert(a > b);assert(a >= b);assert(a < c);assert(a <= c);assert(a < d);assert(a <= d);writeln(a);// eassert(a <= e);assert(a >= e);assert(f < g);assert(g <= g);
toHash() const;opCast(T : const(void[]))() inout;opCast(T : const(size_t[]))() inout;import std.array : array;import std.range : repeat, take;// bit array with 300 elementsauto a = BitArray(true.repeat.take(300).array);size_t[] v =cast(size_t[]) a;const blockSize = size_t.sizeof * 8;writeln(v.length);// (a.length + blockSize - 1) / blockSize
opUnary(string op)() constbool[] ba = [1,0,1,0,1];auto a = BitArray(ba);BitArray b = ~a;writeln(b[0]);// 0writeln(b[1]);// 1writeln(b[2]);// 0writeln(b[3]);// 1writeln(b[4]);// 0
opBinary(string op)(const BitArraye2) conststaticbool[] ba = [1,0,1,0,1];staticbool[] bb = [1,0,1,1,0];auto a = BitArray(ba);auto b = BitArray(bb);BitArray c = a & b;writeln(c[0]);// 1writeln(c[1]);// 0writeln(c[2]);// 1writeln(c[3]);// 0writeln(c[4]);// 0
bool[] ba = [1,0,1,0,1];bool[] bb = [1,0,1,1,0];auto a = BitArray(ba);auto b = BitArray(bb);BitArray c = a | b;writeln(c[0]);// 1writeln(c[1]);// 0writeln(c[2]);// 1writeln(c[3]);// 1writeln(c[4]);// 1
bool[] ba = [1,0,1,0,1];bool[] bb = [1,0,1,1,0];auto a = BitArray(ba);auto b = BitArray(bb);BitArray c = a ^ b;writeln(c[0]);// 0writeln(c[1]);// 0writeln(c[2]);// 0writeln(c[3]);// 1writeln(c[4]);// 1
bool[] ba = [1,0,1,0,1];bool[] bb = [1,0,1,1,0];auto a = BitArray(ba);auto b = BitArray(bb);BitArray c = a - b;writeln(c[0]);// 0writeln(c[1]);// 0writeln(c[2]);// 0writeln(c[3]);// 0writeln(c[4]);// 1
opOpAssign(string op)(const BitArraye2) return scopebool[] ba = [1,0,1,0,1,1,0,1,0,1];bool[] bb = [1,0,1,1,0];auto a = BitArray(ba);auto b = BitArray(bb);BitArray c = a;c.length = 5;c &= b;writeln(a[5]);// 1writeln(a[6]);// 0writeln(a[7]);// 1writeln(a[8]);// 0writeln(a[9]);// 1
bool[] ba = [1,0,1,0,1];bool[] bb = [1,0,1,1,0];auto a = BitArray(ba);auto b = BitArray(bb);a &= b;writeln(a[0]);// 1writeln(a[1]);// 0writeln(a[2]);// 1writeln(a[3]);// 0writeln(a[4]);// 0
bool[] ba = [1,0,1,0,1];bool[] bb = [1,0,1,1,0];auto a = BitArray(ba);auto b = BitArray(bb);a |= b;writeln(a[0]);// 1writeln(a[1]);// 0writeln(a[2]);// 1writeln(a[3]);// 1writeln(a[4]);// 1
bool[] ba = [1,0,1,0,1];bool[] bb = [1,0,1,1,0];auto a = BitArray(ba);auto b = BitArray(bb);a ^= b;writeln(a[0]);// 0writeln(a[1]);// 0writeln(a[2]);// 0writeln(a[3]);// 1writeln(a[4]);// 1
bool[] ba = [1,0,1,0,1];bool[] bb = [1,0,1,1,0];auto a = BitArray(ba);auto b = BitArray(bb);a -= b;writeln(a[0]);// 0writeln(a[1]);// 0writeln(a[2]);// 0writeln(a[3]);// 0writeln(a[4]);// 1
opOpAssign(string op)(boolb) return scopeopOpAssign(string op)(BitArrayb) return scopebool[] ba = [1,0,1,0,1];auto a = BitArray(ba);BitArrayb;b = (a ~=true);writeln(a[0]);// 1writeln(a[1]);// 0writeln(a[2]);// 1writeln(a[3]);// 0writeln(a[4]);// 1writeln(a[5]);// 1writeln(b);// a
bool[] ba = [1,0];bool[] bb = [0,1,0];auto a = BitArray(ba);autob = BitArray(bb);BitArray c;c = (a ~=b);writeln(a.length);// 5writeln(a[0]);// 1writeln(a[1]);// 0writeln(a[2]);// 0writeln(a[3]);// 1writeln(a[4]);// 0writeln(c);// a
opBinary(string op)(boolb) constopBinaryRight(string op)(boolb) constopBinary(string op)(BitArrayb) constbool[] ba = [1,0];bool[] bb = [0,1,0];auto a = BitArray(ba);autob = BitArray(bb);BitArray c;c = (a ~b);writeln(c.length);// 5writeln(c[0]);// 1writeln(c[1]);// 0writeln(c[2]);// 0writeln(c[3]);// 1writeln(c[4]);// 0c = (a ~true);writeln(c.length);// 3writeln(c[0]);// 1writeln(c[1]);// 0writeln(c[2]);// 1c = (false ~ a);writeln(c.length);// 3writeln(c[0]);// 0writeln(c[1]);// 1writeln(c[2]);// 0
opOpAssign(string op)(size_tnbits)opOpAssign(string op)(size_tnbits)import std.format : format;auto b = BitArray([1, 1, 0, 0, 1, 0, 1, 0, 1, 1, 0, 1, 1]);b <<= 1;writeln(format("%b", b));// "01100_10101101"b >>= 1;writeln(format("%b", b));// "11001_01011010"b <<= 4;writeln(format("%b", b));// "00001_10010101"b >>= 5;writeln(format("%b", b));// "10010_10100000"b <<= 13;writeln(format("%b", b));// "00000_00000000"b = BitArray([1, 0, 1, 1, 0, 1, 1, 1]);b >>= 8;writeln(format("%b", b));// "00000000"
toString(W)(ref Wsink, ref scope const FormatSpec!charfmt) constWsink | Achar acceptingoutput range. |
FormatSpec!charfmt | Astd.format.FormatSpec which controls how the data is displayed. |
import std.format : format;auto b = BitArray([0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]);auto s1 = format("%s", b);writeln(s1);// "[0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]"auto s2 = format("%b", b);writeln(s2);// "00001111_00001111"
bitsSet() const;import std.algorithm.comparison : equal;auto b1 = BitArray([0, 0, 0, 0, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1]);assert(b1.bitsSet.equal([4, 5, 6, 7, 12, 13, 14, 15]));BitArray b2;b2.length = 1000;b2[333] =true;b2[666] =true;b2[999] =true;assert(b2.bitsSet.equal([333, 666, 999]));
swapEndian(T)(const Tval)writeln(42.swapEndian);// 704643072assert(42.swapEndian.swapEndian == 42);// reflexivewriteln(1.swapEndian);// 16777216writeln(true.swapEndian);// truewriteln(byte(10).swapEndian);// 10writeln(char(10).swapEndian);// 10writeln(ushort(10).swapEndian);// 2560writeln(long(10).swapEndian);// 720575940379279360writeln(ulong(10).swapEndian);// 720575940379279360
nativeToBigEndian(T)(const Tval)int i = 12345;ubyte[4] swappedI =nativeToBigEndian(i);writeln(i);// bigEndianToNative!int(swappedI)float f = 123.45f;ubyte[4] swappedF =nativeToBigEndian(f);writeln(f);// bigEndianToNative!float(swappedF)constfloat cf = 123.45f;ubyte[4] swappedCF =nativeToBigEndian(cf);writeln(cf);// bigEndianToNative!float(swappedCF)double d = 123.45;ubyte[8] swappedD =nativeToBigEndian(d);writeln(d);// bigEndianToNative!double(swappedD)constdouble cd = 123.45;ubyte[8] swappedCD =nativeToBigEndian(cd);writeln(cd);// bigEndianToNative!double(swappedCD)
bigEndianToNative(T, size_t n)(ubyte[n]val)ushort i = 12345;ubyte[2] swappedI = nativeToBigEndian(i);writeln(i);// bigEndianToNative!ushort(swappedI)dchar c = 'D';ubyte[4] swappedC = nativeToBigEndian(c);writeln(c);// bigEndianToNative!dchar(swappedC)
nativeToLittleEndian(T)(const Tval)int i = 12345;ubyte[4] swappedI =nativeToLittleEndian(i);writeln(i);// littleEndianToNative!int(swappedI)float f = 123.45f;ubyte[4] swappedF =nativeToLittleEndian(f);writeln(f);// littleEndianToNative!float(swappedF)constfloat cf = 123.45f;ubyte[4] swappedCF =nativeToLittleEndian(cf);writeln(cf);// littleEndianToNative!float(swappedCF)double d = 123.45;ubyte[8] swappedD =nativeToLittleEndian(d);writeln(d);// littleEndianToNative!double(swappedD)constdouble cd = 123.45;ubyte[8] swappedCD =nativeToLittleEndian(cd);writeln(cd);// littleEndianToNative!double(swappedCD)
littleEndianToNative(T, size_t n)(ubyte[n]val)ushort i = 12345;ubyte[2] swappedI = nativeToLittleEndian(i);writeln(i);// littleEndianToNative!ushort(swappedI)dchar c = 'D';ubyte[4] swappedC = nativeToLittleEndian(c);writeln(c);// littleEndianToNative!dchar(swappedC)
peek(T, Endian endianness = Endian.bigEndian, R)(Rrange)peek(T, Endian endianness = Endian.bigEndian, R)(Rrange, size_tindex)peek(T, Endian endianness = Endian.bigEndian, R)(Rrange, size_t*index)| T | The integral type to convert the firstT.sizeof bytes to. |
| endianness | The endianness that the bytes are assumed to be in. |
Rrange | The range to read from. |
size_tindex | The index to start reading from (instead of starting at the front). If index is a pointer, then it is updated to the index after the bytes read. The overloads with index are only available ifhasSlicing!R istrue. |
ubyte[] buffer = [1, 5, 22, 9, 44, 255, 8];writeln(buffer.peek!uint());// 17110537writeln(buffer.peek!ushort());// 261writeln(buffer.peek!ubyte());// 1writeln(buffer.peek!uint(2));// 369700095writeln(buffer.peek!ushort(2));// 5641writeln(buffer.peek!ubyte(2));// 22size_tindex = 0;writeln(buffer.peek!ushort(&index));// 261writeln(index);// 2writeln(buffer.peek!uint(&index));// 369700095writeln(index);// 6writeln(buffer.peek!ubyte(&index));// 8writeln(index);// 7
import std.algorithm.iteration : filter;ubyte[] buffer = [1, 5, 22, 9, 44, 255, 7];autorange = filter!"true"(buffer);writeln(range.peek!uint());// 17110537writeln(range.peek!ushort());// 261writeln(range.peek!ubyte());// 1
read(T, Endian endianness = Endian.bigEndian, R)(ref Rrange)| T | The integral type to convert the firstT.sizeof bytes to. |
| endianness | The endianness that the bytes are assumed to be in. |
Rrange | The range to read from. |
import std.range.primitives : empty;ubyte[] buffer = [1, 5, 22, 9, 44, 255, 8];writeln(buffer.length);// 7writeln(buffer.read!ushort());// 261writeln(buffer.length);// 5writeln(buffer.read!uint());// 369700095writeln(buffer.length);// 1writeln(buffer.read!ubyte());// 8assert(buffer.empty);
write(T, Endian endianness = Endian.bigEndian, R)(Rrange, const Tvalue, size_tindex)write(T, Endian endianness = Endian.bigEndian, R)(Rrange, const Tvalue, size_t*index)| T | The integral type to convert the firstT.sizeof bytes to. |
| endianness | The endianness to write the bytes in. |
Rrange | The range to write to. |
Tvalue | The value to write. |
size_tindex | The index to start writing to. If index is a pointer, then it is updated to the index after the bytes read. |
ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0];buffer.write!uint(29110231u, 0);writeln(buffer);// [1, 188, 47, 215, 0, 0, 0, 0]buffer.write!ushort(927, 0);writeln(buffer);// [3, 159, 47, 215, 0, 0, 0, 0]buffer.write!ubyte(42, 0);writeln(buffer);// [42, 159, 47, 215, 0, 0, 0, 0]
ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0];buffer.write!uint(142700095u, 2);writeln(buffer);// [0, 0, 8, 129, 110, 63, 0, 0, 0]buffer.write!ushort(19839, 2);writeln(buffer);// [0, 0, 77, 127, 110, 63, 0, 0, 0]buffer.write!ubyte(132, 2);writeln(buffer);// [0, 0, 132, 127, 110, 63, 0, 0, 0]
ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0];size_tindex = 0;buffer.write!ushort(261, &index);writeln(buffer);// [1, 5, 0, 0, 0, 0, 0, 0]writeln(index);// 2buffer.write!uint(369700095u, &index);writeln(buffer);// [1, 5, 22, 9, 44, 255, 0, 0]writeln(index);// 6buffer.write!ubyte(8, &index);writeln(buffer);// [1, 5, 22, 9, 44, 255, 8, 0]writeln(index);// 7
ubyte[] buffer = [0, 0];buffer.write!bool(false, 0);writeln(buffer);// [0, 0]buffer.write!bool(true, 0);writeln(buffer);// [1, 0]buffer.write!bool(true, 1);writeln(buffer);// [1, 1]buffer.write!bool(false, 1);writeln(buffer);// [1, 0]size_tindex = 0;buffer.write!bool(false, &index);writeln(buffer);// [0, 0]writeln(index);// 1buffer.write!bool(true, &index);writeln(buffer);// [0, 1]writeln(index);// 2
ubyte[] buffer = [0, 0, 0];buffer.write!char('a', 0);writeln(buffer);// [97, 0, 0]buffer.write!char('b', 1);writeln(buffer);// [97, 98, 0]size_tindex = 0;buffer.write!char('a', &index);writeln(buffer);// [97, 98, 0]writeln(index);// 1buffer.write!char('b', &index);writeln(buffer);// [97, 98, 0]writeln(index);// 2buffer.write!char('c', &index);writeln(buffer);// [97, 98, 99]writeln(index);// 3
ubyte[] buffer = [0, 0, 0, 0];buffer.write!wchar('ą', 0);writeln(buffer);// [1, 5, 0, 0]buffer.write!wchar('”', 2);writeln(buffer);// [1, 5, 32, 29]size_tindex = 0;buffer.write!wchar('ć', &index);writeln(buffer);// [1, 7, 32, 29]writeln(index);// 2buffer.write!wchar('ą', &index);writeln(buffer);// [1, 7, 1, 5]writeln(index);// 4
ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0];buffer.write!dchar('ą', 0);writeln(buffer);// [0, 0, 1, 5, 0, 0, 0, 0]buffer.write!dchar('”', 4);writeln(buffer);// [0, 0, 1, 5, 0, 0, 32, 29]size_tindex = 0;buffer.write!dchar('ć', &index);writeln(buffer);// [0, 0, 1, 7, 0, 0, 32, 29]writeln(index);// 4buffer.write!dchar('ą', &index);writeln(buffer);// [0, 0, 1, 7, 0, 0, 1, 5]writeln(index);// 8
ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0];buffer.write!float(32.0f, 0);writeln(buffer);// [66, 0, 0, 0, 0, 0, 0, 0]buffer.write!float(25.0f, 4);writeln(buffer);// [66, 0, 0, 0, 65, 200, 0, 0]size_tindex = 0;buffer.write!float(25.0f, &index);writeln(buffer);// [65, 200, 0, 0, 65, 200, 0, 0]writeln(index);// 4buffer.write!float(32.0f, &index);writeln(buffer);// [65, 200, 0, 0, 66, 0, 0, 0]writeln(index);// 8
ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];buffer.write!double(32.0, 0);writeln(buffer);// [64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]buffer.write!double(25.0, 8);writeln(buffer);// [64, 64, 0, 0, 0, 0, 0, 0, 64, 57, 0, 0, 0, 0, 0, 0]size_tindex = 0;buffer.write!double(25.0, &index);writeln(buffer);// [64, 57, 0, 0, 0, 0, 0, 0, 64, 57, 0, 0, 0, 0, 0, 0]writeln(index);// 8buffer.write!double(32.0, &index);writeln(buffer);// [64, 57, 0, 0, 0, 0, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0]writeln(index);// 16
ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];enum Foo{ one = 10, two = 20, three = 30}buffer.write!Foo(Foo.one, 0);writeln(buffer);// [0, 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0]buffer.write!Foo(Foo.two, 4);writeln(buffer);// [0, 0, 0, 10, 0, 0, 0, 20, 0, 0, 0, 0]buffer.write!Foo(Foo.three, 8);writeln(buffer);// [0, 0, 0, 10, 0, 0, 0, 20, 0, 0, 0, 30]size_tindex = 0;buffer.write!Foo(Foo.three, &index);writeln(buffer);// [0, 0, 0, 30, 0, 0, 0, 20, 0, 0, 0, 30]writeln(index);// 4buffer.write!Foo(Foo.one, &index);writeln(buffer);// [0, 0, 0, 30, 0, 0, 0, 10, 0, 0, 0, 30]writeln(index);// 8buffer.write!Foo(Foo.two, &index);writeln(buffer);// [0, 0, 0, 30, 0, 0, 0, 10, 0, 0, 0, 20]writeln(index);// 12
ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0];enum Float:float{ one = 32.0f, two = 25.0f}buffer.write!Float(Float.one, 0);writeln(buffer);// [66, 0, 0, 0, 0, 0, 0, 0]buffer.write!Float(Float.two, 4);writeln(buffer);// [66, 0, 0, 0, 65, 200, 0, 0]size_tindex = 0;buffer.write!Float(Float.two, &index);writeln(buffer);// [65, 200, 0, 0, 65, 200, 0, 0]writeln(index);// 4buffer.write!Float(Float.one, &index);writeln(buffer);// [65, 200, 0, 0, 66, 0, 0, 0]writeln(index);// 8
ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];enum Double:double{ one = 32.0, two = 25.0}buffer.write!Double(Double.one, 0);writeln(buffer);// [64, 64, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]buffer.write!Double(Double.two, 8);writeln(buffer);// [64, 64, 0, 0, 0, 0, 0, 0, 64, 57, 0, 0, 0, 0, 0, 0]size_tindex = 0;buffer.write!Double(Double.two, &index);writeln(buffer);// [64, 57, 0, 0, 0, 0, 0, 0, 64, 57, 0, 0, 0, 0, 0, 0]writeln(index);// 8buffer.write!Double(Double.one, &index);writeln(buffer);// [64, 57, 0, 0, 0, 0, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0]writeln(index);// 16
ubyte[] buffer = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];enum Real:real{ one = 32.0, two = 25.0}staticassert(!__traits(compiles, buffer.write!Real(Real.one)));
append(T, Endian endianness = Endian.bigEndian, R)(Rrange, const Tvalue)| T | The integral type to convert the firstT.sizeof bytes to. |
| endianness | The endianness to write the bytes in. |
Rrange | The range to append to. |
Tvalue | The value to append. |
import std.array;auto buffer = appender!(constubyte[])();buffer.append!ushort(261);writeln(buffer.data);// [1, 5]buffer.append!uint(369700095u);writeln(buffer.data);// [1, 5, 22, 9, 44, 255]buffer.append!ubyte(8);writeln(buffer.data);// [1, 5, 22, 9, 44, 255, 8]
import std.array : appender;auto buffer = appender!(constubyte[])();buffer.append!bool(true);writeln(buffer.data);// [1]buffer.append!bool(false);writeln(buffer.data);// [1, 0]
import std.array : appender;auto buffer = appender!(constubyte[])();buffer.append!char('a');writeln(buffer.data);// [97]buffer.append!char('b');writeln(buffer.data);// [97, 98]buffer.append!wchar('ą');writeln(buffer.data);// [97, 98, 1, 5]buffer.append!dchar('ą'); writeln(buffer.data);// [97, 98, 1, 5, 0, 0, 1, 5]
import std.array : appender;auto buffer = appender!(constubyte[])();buffer.append!float(32.0f);writeln(buffer.data);// [66, 0, 0, 0]buffer.append!double(32.0);writeln(buffer.data);// [66, 0, 0, 0, 64, 64, 0, 0, 0, 0, 0, 0]
import std.array : appender;auto buffer = appender!(constubyte[])();enum Foo{ one = 10, two = 20, three = 30}buffer.append!Foo(Foo.one);writeln(buffer.data);// [0, 0, 0, 10]buffer.append!Foo(Foo.two);writeln(buffer.data);// [0, 0, 0, 10, 0, 0, 0, 20]buffer.append!Foo(Foo.three);writeln(buffer.data);// [0, 0, 0, 10, 0, 0, 0, 20, 0, 0, 0, 30]
import std.array : appender;auto buffer = appender!(constubyte[])();enum Bool:bool{ bfalse =false, btrue =true,}buffer.append!Bool(Bool.btrue);writeln(buffer.data);// [1]buffer.append!Bool(Bool.bfalse);writeln(buffer.data);// [1, 0]buffer.append!Bool(Bool.btrue);writeln(buffer.data);// [1, 0, 1]
import std.array : appender;auto buffer = appender!(constubyte[])();enum Float:float{ one = 32.0f, two = 25.0f}buffer.append!Float(Float.one);writeln(buffer.data);// [66, 0, 0, 0]buffer.append!Float(Float.two);writeln(buffer.data);// [66, 0, 0, 0, 65, 200, 0, 0]
import std.array : appender;auto buffer = appender!(constubyte[])();enum Double:double{ one = 32.0, two = 25.0}buffer.append!Double(Double.one);writeln(buffer.data);// [64, 64, 0, 0, 0, 0, 0, 0]buffer.append!Double(Double.two);writeln(buffer.data);// [64, 64, 0, 0, 0, 0, 0, 0, 64, 57, 0, 0, 0, 0, 0, 0]
import std.array : appender;auto buffer = appender!(constubyte[])();enum Real:real{ one = 32.0, two = 25.0}staticassert(!__traits(compiles, buffer.append!Real(Real.one)));
bitsSet(T)(const Tvalue)value.Index 0 corresponds to the least significant bit.For signed integers, the highest index corresponds to the sign bit.import std.algorithm.comparison : equal;import std.range : iota;assert(bitsSet(1).equal([0]));assert(bitsSet(5).equal([0, 2]));assert(bitsSet(-1).equal(iota(32)));assert(bitsSet(int.min).equal([31]));