Movatterモバイル変換


[0]ホーム

URL:


D Logo
Menu
Search

Library Reference

version 2.112.0

overview

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 usinga local clone.

std.bitmanip

Bit-level manipulation facilities.
CategoryFunctions
Bit constructsBitArraybitfieldsbitsSet
Endianness conversionbigEndianToNativelittleEndianToNativenativeToBigEndiannativeToLittleEndianswapEndian
Integral rangesappendpeekreadwrite
Floating-Point manipulationDoubleRepFloatRep
TaggingtaggedClassReftaggedPointer
License:
Boost License 1.0.
Authors:
Walter Bright,Andrei Alexandrescu,Jonathan M Davis, Alex Rønne Petersen, Damian Ziemba, Amaury SECHET

Sourcestd/bitmanip.d

stringbitfields(T...)();
Allows creatingbitfields insidestructs,classes andunions.
Abitfield consists of one or more entries with a fixed number ofbits reserved for each of the entries. The types of the entries canbebools, integral types or enumerated types, arbitrarily mixed.The most efficient type to store inbitfields 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.

Parameters:
TA 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.
Returns:
A string that can be used in amixin to add thebitfield.
See Also:
Examples:
Create abitfield pack of eight bits, which fit inoneubyte. Thebitfields 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
Examples:
The sum of all bit lengths in onebitfield instantiationmust be exactly 8, 16, 32, or 64. If padding is needed, just allocateone bitfield with an empty name.
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
Examples:
enums can be used too
enum ABC { A, B, C }struct EnumTest{mixin(bitfields!(              ABC,"x", 2,bool,"y", 1,ubyte,"z", 5));}
enum autotaggedPointer(T : T*, string name, Ts...);
This string mixin generator allows one to create tagged pointers insidestructs andclasses.
A tagged pointer uses the bits known to be zero in a normal pointer or class reference to store extra information.For example, a pointer to an integer must be 4-byte aligned, so there are 2 bits that are always known to be zero.One can store a 2-bit integer there.
The example above creates a tagged pointer in the struct A. The pointer is of typeuint* as specified by the first argument, and is named x, as specified by the secondargument.
Following arguments works the same way asbitfield's. The bitfield must fit into thebits known to be zero because of the pointer alignment.
Examples:
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;
templatetaggedClassRef(T, string name, Ts...) if (is(T == class))
This string mixin generator allows one to create tagged class reference insidestructs andclasses.
A tagged class reference uses the bits known to be zero in a normal class reference to store extra information.For example, a pointer to an integer must be 4-byte aligned, so there are 2 bits that are always known to be zero.One can store a 2-bit integer there.
The example above creates a tagged reference to an Object in the struct A. This expects the same parametersastaggedPointer, except the first argument which must be a class type instead of a pointer type.
Examples:
struct A{int a;mixin(taggedClassRef!(        Object,"o",uint,"i", 2));}A obj;obj.o =new Object();obj.i = 3;
aliasFloatRep = FloatingPointRepresentation!float.FloatingPointRepresentation;
Allows manipulating the fraction, exponent, and sign parts of afloat separately. The definition is:
structFloatRep{union    {float value;mixin(bitfields!(uint,"fraction", 23,ubyte,"exponent",  8,bool,"sign",      1));    }enumuint bias = 127, fractionBits = 23, exponentBits = 8, signBits = 1;}
Examples:
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
Examples:
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
Examples:
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);
aliasDoubleRep = FloatingPointRepresentation!double.FloatingPointRepresentation;
Allows manipulating the fraction, exponent, and sign parts of adouble separately. The definition is:
structDoubleRep{union    {double value;mixin(bitfields!(ulong,"fraction", 52,ushort,"exponent", 11,bool,"sign",      1));    }enumuint bias = 1023, signBits = 1, fractionBits = 52, exponentBits = 11;}
Examples:
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
Examples:
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
Examples:
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);
Examples:
Reading
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);
Examples:
Writing
DoubleRep x;x.fraction = 1125899906842624;x.exponent = 1025;x.sign =true;writeln(x.value);// -5.0
structBitArray;
A dynamic array of bits. Each bit in aBitArray 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.
Examples:
Slicing & bitsSet
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)));
Examples:
Concatenation and appending
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]));
Examples:
Bit flipping
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]));
Examples:
String format of bitarrays
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"
Examples:
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"
pure nothrow this(in bool[]ba);
Creates aBitArray from abool array, such thatbool values read from left to right correspond to subsequent bits in theBitArray.
Parameters:
bool[]baSource array ofbool values.
Examples:
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]));
Examples:
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)));
pure nothrow @nogc this(void[]v, size_tnumbits);
Creates aBitArray from the raw contents of the source array. The source array is not copied but simply acts as the underlying array of bits, which stores data assize_t units.
That means a particular care should be taken when passing an array of a type different thansize_t, firstly because its length should be a multiple ofsize_t.sizeof, and secondly because how the bits are mapped:
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);}
The least significant bit in anysize_t unit is the starting bit of this unit, and the most significant bit is the last bit of this unit. Therefore, passing e.g. an array ofints may result in a differentBitArray depending on the processor's endianness.
This constructor is the inverse ofopCast.
Parameters:
void[]vSource array.v.length must be a multple ofsize_t.sizeof.
size_tnumbitsNumber of bits to be mapped from the source array, i.e. length of the createdBitArray.
Examples:
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
Examples:
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]));
Examples:
// 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
pure nothrow @nogc @property @safe size_tdim() const;
Returns:
Dimension i.e. the number of native words backing thisBitArray.
Technically, this is the length of the underlying array storing bits, which is equal toceil(length / (size_t.sizeof * 8)), as bits are packed intosize_t units.
pure nothrow @nogc @property @safe size_tlength() const;
Returns:
Number of bits in theBitArray.
pure nothrow @property @system size_tlength(size_tnewlen);
Sets the amount of bits in theBitArray.Warning: increasing length may overwrite bits in the final word of the current underlying data regardless of whether it is shared between BitArray objects. i.e. D dynamic array extension semantics are not followed.
pure nothrow @nogc boolopIndex(size_ti) const;
Gets thei'th bit in theBitArray.
Examples:
staticvoid fun(const BitArray arr){auto x = arr[0];    writeln(x);// 1}BitArray a;a.length = 3;a[0] = 1;fun(a);
pure nothrow @nogc boolopIndexAssign(boolb, size_ti);
Sets thei'th bit in theBitArray.
pure nothrow @nogc voidopSliceAssign(boolval);
Sets all the values in theBitArray to the value specified byval.
Examples:
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);
pure nothrow @nogc voidopSliceAssign(boolval, size_tstart, size_tend);
Sets the bits of a slice ofBitArray starting at indexstart and ends at indexend - 1 with the values specified byval.
Examples:
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);
pure nothrow @nogc voidflip();
Flips all the bits in theBitArray
Examples:
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)));
pure nothrow @nogc voidflip(size_tpos);
Flips a single bit, specified bypos
Examples:
auto 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
pure nothrow @nogc @safe size_tcount() const scope;
Counts all the set bits in theBitArray
Examples:
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
pure nothrow @property BitArraydup() const;
Duplicates theBitArray and its contents.
Examples:
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)
intopApply(scope int delegate(ref bool)dg);

intopApply(scope int delegate(bool)dg) const;

intopApply(scope int delegate(size_t, ref bool)dg);

intopApply(scope int delegate(size_t, bool)dg) const;
Support forforeach loops forBitArray.
Examples:
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);    }}
pure nothrow @nogc @property BitArrayreverse() return;
Reverses the bits of theBitArray.
Examples:
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]
pure nothrow @nogc @property BitArraysort() return;
Sorts theBitArray's elements.
Examples:
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
pure nothrow @nogc boolopEquals(ref const BitArraya2) const;
Support for operators == and != forBitArray.
Examples:
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);
pure nothrow @nogc intopCmp(BitArraya2) const;
Supports comparison operators forBitArray.
Examples:
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);
pure nothrow @nogc size_ttoHash() const;
Support for hashing forBitArray.
pure nothrow @nogc inout(void)[]opCast(T : const(void[]))() inout;
Convert tovoid[].
pure nothrow @nogc inout(size_t)[]opCast(T : const(size_t[]))() inout;
Convert tosize_t[].
Examples:
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
pure nothrow BitArrayopUnary(string op)() const
if (op == "~");
Support for unary operator ~ forBitArray.
Examples:
bool[] 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
pure nothrow BitArrayopBinary(string op)(const BitArraye2) const
if (op == "-" || op == "&" || op == "|" || op == "^");
Support for binary bitwise operators forBitArray.
Examples:
staticbool[] 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
Examples:
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
Examples:
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
Examples:
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
pure nothrow @nogc BitArrayopOpAssign(string op)(const BitArraye2) return scope
if (op == "-" || op == "&" || op == "|" || op == "^");
Support for operator op= forBitArray.
Examples:
bool[] 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
Examples:
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
Examples:
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
Examples:
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
Examples:
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
pure nothrow BitArrayopOpAssign(string op)(boolb) return scope
if (op == "~");

pure nothrow BitArrayopOpAssign(string op)(BitArrayb) return scope
if (op == "~");
Support for operator ~= forBitArray.Warning: This will overwrite a bit in the final word of the current underlying data regardless of whether it is shared between BitArray objects. i.e. D dynamic array concatenation semantics are not followed
Examples:
bool[] 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
Examples:
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
pure nothrow BitArrayopBinary(string op)(boolb) const
if (op == "~");

pure nothrow BitArrayopBinaryRight(string op)(boolb) const
if (op == "~");

pure nothrow BitArrayopBinary(string op)(BitArrayb) const
if (op == "~");
Support for binary operator ~ forBitArray.
Examples:
bool[] 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
pure nothrow @nogc voidopOpAssign(string op)(size_tnbits)
if (op == "<<");
Operator<<= support.
Shifts all the bits in the array to the left by the given number of bits. The leftmost bits are dropped, and 0's are appended to the end to fill up the vacant bits.
Warning: unused bits in the final word up to the next word boundary may be overwritten by this operation. It does not attempt to preserve bits past the end of the array.
pure nothrow @nogc voidopOpAssign(string op)(size_tnbits)
if (op == ">>");
Operator>>= support.
Shifts all the bits in the array to the right by the given number of bits. The rightmost bits are dropped, and 0's are inserted at the back to fill up the vacant bits.
Warning: unused bits in the final word up to the next word boundary may be overwritten by this operation. It does not attempt to preserve bits past the end of the array.
Examples:
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"
voidtoString(W)(ref Wsink, ref scope const FormatSpec!charfmt) const
if (isOutputRange!(W, char));
Return a string representation of this BitArray.
Two format specifiers are supported:
  • %s which prints the bits as an array, and
  • %b which prints the bits as 8-bit byte packets
  • separated with an underscore.
    Parameters:
    WsinkAchar acceptingoutput range.
    FormatSpec!charfmtAstd.format.FormatSpec which controls how the data is displayed.
    Examples:
    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"
    nothrow @property autobitsSet() const;
    Return a lazy range of the indices of set bits.
    Examples:
    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]));
    pure nothrow @nogc @safe TswapEndian(T)(const Tval)
    if (isIntegral!T || isSomeChar!T || isBoolean!T);
    Swaps the endianness of the given integral value or character.
    Examples:
    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
    pure nothrow @nogc @trusted autonativeToBigEndian(T)(const Tval)
    if (canSwapEndianness!T);
    Converts the given value from the native endianness to big endian and returns it as aubyte[n] wheren is the size of the given type.
    Returning aubyte[n] helps prevent accidentally using a swapped value as a regular one (and in the case of floating point values, it's necessary, because the FPU will mess up any swapped floating point values. So, you can't actually have swapped floating point values as floating point values).
    real is not supported, because its size is implementation-dependent and therefore could vary from machine to machine (which could make it unusable if you tried to transfer it to another machine).
    Examples:
    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)
    pure nothrow @nogc @trusted TbigEndianToNative(T, size_t n)(ubyte[n]val)
    if (canSwapEndianness!T && (n == T.sizeof));
    Converts the given value from big endian to the native endianness and returns it. The value is given as aubyte[n] wheren is the size of the target type. You must give the target type as a template argument, because there are multiple types with the same size and so the type of the argument is not enough to determine the return type.
    Taking aubyte[n] helps prevent accidentally using a swapped value as a regular one (and in the case of floating point values, it's necessary, because the FPU will mess up any swapped floating point values. So, you can't actually have swapped floating point values as floating point values).
    Examples:
    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)
    pure nothrow @nogc @trusted autonativeToLittleEndian(T)(const Tval)
    if (canSwapEndianness!T);
    Converts the given value from the native endianness to little endian and returns it as aubyte[n] wheren is the size of the given type.
    Returning aubyte[n] helps prevent accidentally using a swapped value as a regular one (and in the case of floating point values, it's necessary, because the FPU will mess up any swapped floating point values. So, you can't actually have swapped floating point values as floating point values).
    Examples:
    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)
    pure nothrow @nogc @trusted TlittleEndianToNative(T, size_t n)(ubyte[n]val)
    if (canSwapEndianness!T && (n == T.sizeof));
    Converts the given value from little endian to the native endianness and returns it. The value is given as aubyte[n] wheren is the size of the target type. You must give the target type as a template argument, because there are multiple types with the same size and so the type of the argument is not enough to determine the return type.
    Taking aubyte[n] helps prevent accidentally using a swapped value as a regular one (and in the case of floating point values, it's necessary, because the FPU will mess up any swapped floating point values. So, you can't actually have swapped floating point values as floating point values).
    real is not supported, because its size is implementation-dependent and therefore could vary from machine to machine (which could make it unusable if you tried to transfer it to another machine).
    Examples:
    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)
    Tpeek(T, Endian endianness = Endian.bigEndian, R)(Rrange)
    if (canSwapEndianness!T && isForwardRange!R && is(ElementType!R : const(ubyte)));

    Tpeek(T, Endian endianness = Endian.bigEndian, R)(Rrange, size_tindex)
    if (canSwapEndianness!T && isForwardRange!R && hasSlicing!R && is(ElementType!R : const(ubyte)));

    Tpeek(T, Endian endianness = Endian.bigEndian, R)(Rrange, size_t*index)
    if (canSwapEndianness!T && isForwardRange!R && hasSlicing!R && is(ElementType!R : const(ubyte)));
    Takes a range ofubytes and converts the firstT.sizeof bytes toT. The value returned is converted from the given endianness to the native endianness. The range is not consumed.
    Parameters:
    TThe integral type to convert the firstT.sizeof bytes to.
    endiannessThe endianness that the bytes are assumed to be in.
    RrangeThe range to read from.
    size_tindexThe 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.
    Examples:
    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
    Examples:
    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
    Tread(T, Endian endianness = Endian.bigEndian, R)(ref Rrange)
    if (canSwapEndianness!T && isInputRange!R && is(ElementType!R : const(ubyte)));
    Takes a range ofubytes and converts the firstT.sizeof bytes toT. The value returned is converted from the given endianness to the native endianness. TheT.sizeof bytes which are read are consumed from the range.
    Parameters:
    TThe integral type to convert the firstT.sizeof bytes to.
    endiannessThe endianness that the bytes are assumed to be in.
    RrangeThe range to read from.
    Examples:
    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);
    voidwrite(T, Endian endianness = Endian.bigEndian, R)(Rrange, const Tvalue, size_tindex)
    if (canSwapEndianness!T && isForwardRange!R && hasSlicing!R && is(ElementType!R : ubyte));

    voidwrite(T, Endian endianness = Endian.bigEndian, R)(Rrange, const Tvalue, size_t*index)
    if (canSwapEndianness!T && isForwardRange!R && hasSlicing!R && is(ElementType!R : ubyte));
    Takes an integral value, converts it to the given endianness, and writes it to the given range ofubytes as a sequence ofT.sizeofubytes starting at index.hasSlicing!R must betrue.
    Parameters:
    TThe integral type to convert the firstT.sizeof bytes to.
    endiannessThe endianness to write the bytes in.
    RrangeThe range to write to.
    TvalueThe value to write.
    size_tindexThe index to start writing to. If index is a pointer, then it is updated to the index after the bytes read.
    Examples:
    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]
    Examples:
    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]
    Examples:
    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
    Examples:
    bool
    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
    Examples:
    char(8-bit)
    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
    Examples:
    wchar (16bit - 2x ubyte)
    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
    Examples:
    dchar (32bit - 4x ubyte)
    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
    Examples:
    float (32bit - 4x ubyte)
    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
    Examples:
    double (64bit - 8x ubyte)
    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
    Examples:
    enum
    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
    Examples:
    enum - float
    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
    Examples:
    enum - double
    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
    Examples:
    enum - real
    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)));
    voidappend(T, Endian endianness = Endian.bigEndian, R)(Rrange, const Tvalue)
    if (canSwapEndianness!T && isOutputRange!(R, ubyte));
    Takes an integral value, converts it to the given endianness, and appends it to the given range ofubytes (usingput) as a sequence ofT.sizeofubytes starting at index.hasSlicing!R must betrue.
    Parameters:
    TThe integral type to convert the firstT.sizeof bytes to.
    endiannessThe endianness to write the bytes in.
    RrangeThe range to append to.
    TvalueThe value to append.
    Examples:
    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]
    Examples:
    bool
    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]
    Examples:
    char wchar dchar
    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]
    Examples:
    float double
    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]
    Examples:
    enum
    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]
    Examples:
    enum - bool
    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]
    Examples:
    enum - float
    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]
    Examples:
    enum - double
    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]
    Examples:
    enum - real
    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)));
    pure nothrow @nogc autobitsSet(T)(const Tvalue)
    if (isIntegral!T);
    Range that iterates the indices of the set bits invalue.Index 0 corresponds to the least significant bit.For signed integers, the highest index corresponds to the sign bit.
    Examples:
    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]));
    Copyright © 1999-2026 by theD Language Foundation | Page generated byDdoc on Sat Feb 21 04:07:55 2026

    [8]ページ先頭

    ©2009-2026 Movatter.jp