Bit-level manipulation facilities.
Category | Functions |
---|---|
Bit constructs | BitArray bitfields bitsSet |
Endianness conversion | bigEndianToNative littleEndianToNative nativeToBigEndian nativeToLittleEndian swapEndian |
Integral ranges | append peek read write |
Floating-Point manipulation | DoubleRep FloatRep |
Tagging | taggedClassRef taggedPointer |
Allows creating bit fields inside structs and classes.
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.
std.typecons.BitFlags
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
struct A { mixin(bitfields!( bool, "flag1", 1, bool, "flag2", 1, uint, "", 6)); }
enum ABC { A, B, C } struct EnumTest { mixin(bitfields!( ABC, "x", 2, bool, "y", 1, ubyte, "z", 5)); }
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
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
This string mixin generator allows one to create tagged pointers inside structs and classes.
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 type uint*
as specified by the first argument, and is named x, as specified by the second argument.
Following arguments works the same way as bitfield
's. The bitfield must fit into the bits known to be zero because of the pointer alignment.
struct A { int a; mixin(taggedPointer!( uint*, "x", bool, "b1", 1, bool, "b2", 1)); } A obj; obj.x = new uint; obj.b1 = true; obj.b2 = false;
This string mixin generator allows one to create tagged class reference inside structs and classes.
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 parameters as taggedPointer
, except the first argument which must be a class type instead of a pointer type.
struct A { int a; mixin(taggedClassRef!( Object, "o", uint, "i", 2)); } A obj; obj.o = new Object(); obj.i = 3;
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)); } enum uint bias = 127, fractionBits = 23, exponentBits = 8, signBits = 1; }
FloatRep rep = {value: 0}; writeln(rep.fraction); // 0 writeln(rep.exponent); // 0 assert(!rep.sign); rep.value = 42; writeln(rep.fraction); // 2621440 writeln(rep.exponent); // 132 assert(!rep.sign); rep.value = 10; writeln(rep.fraction); // 2097152 writeln(rep.exponent); // 130
FloatRep rep = {value: 1}; writeln(rep.fraction); // 0 writeln(rep.exponent); // 127 assert(!rep.sign); rep.exponent = 126; writeln(rep.value); // 0.5 rep.exponent = 130; writeln(rep.value); // 8
FloatRep rep = {value: 1}; rep.value = -0.5; writeln(rep.fraction); // 0 writeln(rep.exponent); // 126 assert(rep.sign); rep.value = -1. / 3; writeln(rep.fraction); // 2796203 writeln(rep.exponent); // 125 assert(rep.sign);
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)); } enum uint bias = 1023, signBits = 1, fractionBits = 52, exponentBits = 11; }
DoubleRep rep = {value: 0}; writeln(rep.fraction); // 0 writeln(rep.exponent); // 0 assert(!rep.sign); rep.value = 42; writeln(rep.fraction); // 1407374883553280 writeln(rep.exponent); // 1028 assert(!rep.sign); rep.value = 10; writeln(rep.fraction); // 1125899906842624 writeln(rep.exponent); // 1026
DoubleRep rep = {value: 1}; writeln(rep.fraction); // 0 writeln(rep.exponent); // 1023 assert(!rep.sign); rep.exponent = 1022; writeln(rep.value); // 0.5 rep.exponent = 1026; writeln(rep.value); // 8
DoubleRep rep = {value: 1}; rep.value = -0.5; writeln(rep.fraction); // 0 writeln(rep.exponent); // 1022 assert(rep.sign); rep.value = -1. / 3; writeln(rep.fraction); // 1501199875790165 writeln(rep.exponent); // 1021 assert(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
A dynamic array of bits. Each bit in a BitArray
can be manipulated individually or by the standard bitwise operators &
, |
, ^
, ~
, >>
, <<
and also by other effective member functions; most of them work relative to the BitArray
's dimension (see dim
), instead of its length
.
import std.algorithm.comparison : equal; import std.range : iota; bool[] buf = new bool[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]); // 1 b ~= BitArray([0, 1]); auto c = BitArray([1, 0, 1, 0, 1]); writeln(b); // c assert(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) is null); 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"
Creates a BitArray
from a bool
array, such that bool
values read from left to right correspond to subsequent bits in the BitArray
.
bool[] ba
| Source array of bool values. |
import std.algorithm.comparison : equal; bool[] input = [true, false, false, true, true]; auto a = BitArray(input); writeln(a.length); // 5 assert(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); // 3 assert(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); // 70 assert(a.bitsSet.equal(iota(0, 70)));
Creates a BitArray
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 as size_t
units.
That means a particular care should be taken when passing an array of a type different than size_t
, firstly because its length should be a multiple of size_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 any
size_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 of int
s may result in a different BitArray
depending on the processor's endianness. opCast
. void[] v
| Source array. v.length must be a multple of size_t.sizeof . |
size_t numbits
| Number of bits to be mapped from the source array, i.e. length of the created BitArray . |
import std.algorithm.comparison : equal; auto a = BitArray([1, 0, 0, 1, 1]); // Inverse of the cast. auto v = cast(void[]) a; auto b = BitArray(v, a.length); writeln(b.length); // 5 assert(b.bitsSet.equal([0, 3, 4])); // a and b share the underlying data. a[0] = 0; writeln(b[0]); // 0 writeln(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. size_t[] source = [1, 0b101, 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))); 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])); // The unmapped bits from the final word have been cleared. writeln(source[1]); // 1
BitArray
. Technically, this is the length of the underlying array storing bits, which is equal to ceil(length / (size_t.sizeof * 8))
, as bits are packed into size_t
units.BitArray
.Sets the amount of bits in the BitArray
. Warning: increasing length may overwrite bits in final word up to the next word boundary. i.e. D dynamic array extension semantics are not followed.
Gets the i
'th bit in the BitArray
.
static void fun(const BitArray arr) { auto x = arr[0]; writeln(x); // 1 } BitArray a; a.length = 3; a[0] = 1; fun(a);
Sets the i
'th bit in the BitArray
.
Sets all the values in the BitArray
to the value specified by val
.
import std.algorithm.comparison : equal; auto b = BitArray([1, 0, 1, 0, 1, 1]); b[] = true; // all bits are set assert(b.bitsSet.equal([0, 1, 2, 3, 4, 5])); b[] = false; // none of the bits are set assert(b.bitsSet.empty);
Sets the bits of a slice of BitArray
starting at index start
and ends at index ($D end - 1) with the values specified by val
.
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);
Flips all the bits in the BitArray
import std.algorithm.comparison : equal; import std.range : iota; // positions 0, 2, 4 are set auto b = BitArray([1, 0, 1, 0, 1, 0]); b.flip(); // after flipping, positions 1, 3, 5 are set assert(b.bitsSet.equal([1, 3, 5])); bool[270] bits; auto b1 = BitArray(bits); b1.flip(); assert(b1.bitsSet.equal(iota(0, 270)));
Flips a single bit, specified by pos
auto ax = BitArray([1, 0, 0, 1]); ax.flip(0); writeln(ax[0]); // 0 bool[200] y; y[90 .. 130] = true; auto ay = BitArray(y); ay.flip(100); writeln(ay[100]); // 0
Counts all the set bits in the BitArray
auto a = BitArray([0, 1, 1, 0, 0, 1, 1]); writeln(a.count); // 4 bool[200] boolArray; boolArray[45 .. 130] = true; auto c = BitArray(boolArray); writeln(c.count); // 85
Duplicates the BitArray
and its contents.
BitArray a; BitArray b; a.length = 3; a[0] = 1; a[1] = 0; a[2] = 1; b = a.dup; writeln(b.length); // 3 foreach (i; 0 .. 3) writeln(b[i]); // (((i ^ 1) & 1) ? true : false)
Support for foreach
loops for BitArray
.
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); } }
Reverses the bits of the BitArray
.
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]
Sorts the BitArray
's elements.
size_t x = 0b1100011000; auto ba = BitArray(10, &x); ba.sort; foreach (i; 0 .. 6) writeln(ba[i]); // false foreach (i; 6 .. 10) writeln(ba[i]); // true
Support for operators == and != for BitArray
.
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); // e assert(f != g);
Supports comparison operators for BitArray
.
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); // e assert(a <= e); assert(a >= e); assert(f < g); assert(g <= g);
Support for hashing for BitArray
.
Convert to void[]
.
Convert to size_t[]
.
import std.array : array; import std.range : repeat, take; // bit array with 300 elements auto 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
Support for unary operator ~ for BitArray
.
bool[] ba = [1,0,1,0,1]; auto a = BitArray(ba); BitArray b = ~a; writeln(b[0]); // 0 writeln(b[1]); // 1 writeln(b[2]); // 0 writeln(b[3]); // 1 writeln(b[4]); // 0
Support for binary bitwise operators for BitArray
.
static bool[] ba = [1,0,1,0,1]; static bool[] bb = [1,0,1,1,0]; auto a = BitArray(ba); auto b = BitArray(bb); BitArray c = a & b; writeln(c[0]); // 1 writeln(c[1]); // 0 writeln(c[2]); // 1 writeln(c[3]); // 0 writeln(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]); // 1 writeln(c[1]); // 0 writeln(c[2]); // 1 writeln(c[3]); // 1 writeln(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]); // 0 writeln(c[1]); // 0 writeln(c[2]); // 0 writeln(c[3]); // 1 writeln(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]); // 0 writeln(c[1]); // 0 writeln(c[2]); // 0 writeln(c[3]); // 0 writeln(c[4]); // 1
Support for operator op= for BitArray
.
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]); // 1 writeln(a[6]); // 0 writeln(a[7]); // 1 writeln(a[8]); // 0 writeln(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]); // 1 writeln(a[1]); // 0 writeln(a[2]); // 1 writeln(a[3]); // 0 writeln(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]); // 1 writeln(a[1]); // 0 writeln(a[2]); // 1 writeln(a[3]); // 1 writeln(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]); // 0 writeln(a[1]); // 0 writeln(a[2]); // 0 writeln(a[3]); // 1 writeln(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]); // 0 writeln(a[1]); // 0 writeln(a[2]); // 0 writeln(a[3]); // 0 writeln(a[4]); // 1
Support for operator ~= for BitArray
. 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
bool[] ba = [1,0,1,0,1]; auto a = BitArray(ba); BitArray b; b = (a ~= true); writeln(a[0]); // 1 writeln(a[1]); // 0 writeln(a[2]); // 1 writeln(a[3]); // 0 writeln(a[4]); // 1 writeln(a[5]); // 1 writeln(b); // a
bool[] ba = [1,0]; bool[] bb = [0,1,0]; auto a = BitArray(ba); auto b = BitArray(bb); BitArray c; c = (a ~= b); writeln(a.length); // 5 writeln(a[0]); // 1 writeln(a[1]); // 0 writeln(a[2]); // 0 writeln(a[3]); // 1 writeln(a[4]); // 0 writeln(c); // a
Support for binary operator ~ for BitArray
.
bool[] ba = [1,0]; bool[] bb = [0,1,0]; auto a = BitArray(ba); auto b = BitArray(bb); BitArray c; c = (a ~ b); writeln(c.length); // 5 writeln(c[0]); // 1 writeln(c[1]); // 0 writeln(c[2]); // 0 writeln(c[3]); // 1 writeln(c[4]); // 0 c = (a ~ true); writeln(c.length); // 3 writeln(c[0]); // 1 writeln(c[1]); // 0 writeln(c[2]); // 1 c = (false ~ a); writeln(c.length); // 3 writeln(c[0]); // 0 writeln(c[1]); // 1 writeln(c[2]); // 0
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.
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.
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"
Return a string representation of this BitArray.
Two format specifiers are supported:
W sink
| A char accepting output range. |
FormatSpec!char fmt
| A std.format.FormatSpec which controls how the data is displayed. |
Return a lazy range of the indices of set bits.
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]));
Swaps the endianness of the given integral value or character.
writeln(42.swapEndian); // 704643072 assert(42.swapEndian.swapEndian == 42); // reflexive writeln(1.swapEndian); // 16777216 writeln(true.swapEndian); // true writeln(byte(10).swapEndian); // 10 writeln(char(10).swapEndian); // 10 writeln(ushort(10).swapEndian); // 2560 writeln(long(10).swapEndian); // 720575940379279360 writeln(ulong(10).swapEndian); // 720575940379279360
Converts the given value from the native endianness to big endian and returns it as a ubyte[n]
where n
is the size of the given type.
Returning a ubyte[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).
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) const float 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) const double cd = 123.45; ubyte[8] swappedCD = nativeToBigEndian(cd); writeln(cd); // bigEndianToNative!double(swappedCD)
Converts the given value from big endian to the native endianness and returns it. The value is given as a ubyte[n]
where n
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 a ubyte[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).
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)
Converts the given value from the native endianness to little endian and returns it as a ubyte[n]
where n
is the size of the given type.
Returning a ubyte[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).
int i = 12345; ubyte[4] swappedI = nativeToLittleEndian(i); writeln(i); // littleEndianToNative!int(swappedI) double d = 123.45; ubyte[8] swappedD = nativeToLittleEndian(d); writeln(d); // littleEndianToNative!double(swappedD)
Converts the given value from little endian to the native endianness and returns it. The value is given as a ubyte[n]
where n
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 a ubyte[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).
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)
Takes a range of ubyte
s and converts the first T.sizeof
bytes to T
. The value returned is converted from the given endianness to the native endianness. The range is not consumed.
T | The integral type to convert the first T.sizeof bytes to. |
endianness | The endianness that the bytes are assumed to be in. |
R range
| The range to read from. |
size_t index
| 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 if hasSlicing!R is true . |
ubyte[] buffer = [1, 5, 22, 9, 44, 255, 8]; writeln(buffer.peek!uint()); // 17110537 writeln(buffer.peek!ushort()); // 261 writeln(buffer.peek!ubyte()); // 1 writeln(buffer.peek!uint(2)); // 369700095 writeln(buffer.peek!ushort(2)); // 5641 writeln(buffer.peek!ubyte(2)); // 22 size_t index = 0; writeln(buffer.peek!ushort(&index)); // 261 writeln(index); // 2 writeln(buffer.peek!uint(&index)); // 369700095 writeln(index); // 6 writeln(buffer.peek!ubyte(&index)); // 8 writeln(index); // 7
import std.algorithm.iteration : filter; ubyte[] buffer = [1, 5, 22, 9, 44, 255, 7]; auto range = filter!"true"(buffer); writeln(range.peek!uint()); // 17110537 writeln(range.peek!ushort()); // 261 writeln(range.peek!ubyte()); // 1
Takes a range of ubyte
s and converts the first T.sizeof
bytes to T
. The value returned is converted from the given endianness to the native endianness. The T.sizeof
bytes which are read are consumed from the range.
T | The integral type to convert the first T.sizeof bytes to. |
endianness | The endianness that the bytes are assumed to be in. |
R range
| The range to read from. |
import std.range.primitives : empty; ubyte[] buffer = [1, 5, 22, 9, 44, 255, 8]; writeln(buffer.length); // 7 writeln(buffer.read!ushort()); // 261 writeln(buffer.length); // 5 writeln(buffer.read!uint()); // 369700095 writeln(buffer.length); // 1 writeln(buffer.read!ubyte()); // 8 assert(buffer.empty);
Takes an integral value, converts it to the given endianness, and writes it to the given range of ubyte
s as a sequence of T.sizeof
ubyte
s starting at index. hasSlicing!R
must be true
.
T | The integral type to convert the first T.sizeof bytes to. |
endianness | The endianness to write the bytes in. |
R range
| The range to write to. |
T value
| The value to write. |
size_t index
| 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_t index = 0; buffer.write!ushort(261, &index); writeln(buffer); // [1, 5, 0, 0, 0, 0, 0, 0] writeln(index); // 2 buffer.write!uint(369700095u, &index); writeln(buffer); // [1, 5, 22, 9, 44, 255, 0, 0] writeln(index); // 6 buffer.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_t index = 0; buffer.write!bool(false, &index); writeln(buffer); // [0, 0] writeln(index); // 1 buffer.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_t index = 0; buffer.write!char('a', &index); writeln(buffer); // [97, 98, 0] writeln(index); // 1 buffer.write!char('b', &index); writeln(buffer); // [97, 98, 0] writeln(index); // 2 buffer.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_t index = 0; buffer.write!wchar('ć', &index); writeln(buffer); // [1, 7, 32, 29] writeln(index); // 2 buffer.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_t index = 0; buffer.write!dchar('ć', &index); writeln(buffer); // [0, 0, 1, 7, 0, 0, 32, 29] writeln(index); // 4 buffer.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_t index = 0; buffer.write!float(25.0f, &index); writeln(buffer); // [65, 200, 0, 0, 65, 200, 0, 0] writeln(index); // 4 buffer.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_t index = 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); // 8 buffer.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_t index = 0; buffer.write!Foo(Foo.three, &index); writeln(buffer); // [0, 0, 0, 30, 0, 0, 0, 20, 0, 0, 0, 30] writeln(index); // 4 buffer.write!Foo(Foo.one, &index); writeln(buffer); // [0, 0, 0, 30, 0, 0, 0, 10, 0, 0, 0, 30] writeln(index); // 8 buffer.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_t index = 0; buffer.write!Float(Float.two, &index); writeln(buffer); // [65, 200, 0, 0, 65, 200, 0, 0] writeln(index); // 4 buffer.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_t index = 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); // 8 buffer.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 } static assert(!__traits(compiles, buffer.write!Real(Real.one)));
Takes an integral value, converts it to the given endianness, and appends it to the given range of ubyte
s (using put
) as a sequence of T.sizeof
ubyte
s starting at index. hasSlicing!R
must be true
.
T | The integral type to convert the first T.sizeof bytes to. |
endianness | The endianness to write the bytes in. |
R range
| The range to append to. |
T value
| The value to append. |
import std.array; auto buffer = appender!(const ubyte[])(); 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!(const ubyte[])(); buffer.append!bool(true); writeln(buffer.data); // [1] buffer.append!bool(false); writeln(buffer.data); // [1, 0]
import std.array : appender; auto buffer = appender!(const ubyte[])(); 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!(const ubyte[])(); 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!(const ubyte[])(); 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!(const ubyte[])(); 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!(const ubyte[])(); 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!(const ubyte[])(); 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!(const ubyte[])(); enum Real: real { one = 32.0, two = 25.0 } static assert(!__traits(compiles, buffer.append!Real(Real.one)));
Range that iterates the indices of the set bits in 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]));
© 1999–2018 The D Language Foundation
Licensed under the Boost License 1.0.
https://dlang.org/phobos/std_bitmanip.html