英語版
このページの英語版を見る

std.bitmanip

ビットレベルの操作機能。
カテゴリー 関数" 区分
ビット構成要素 BitArray bitfields bitsSet
エンディアン変換 bigEndianToNative littleEndianToNative nativeToBigEndian nativeToLittleEndian swapEndian
積分の範囲 append peek read write
浮動小数点演算 DoubleRep FloatRep
タグ付け taggedClassRef taggedPointer
Authors:
Walter Bright, Andrei Alexandrescu, Jonathan M Davis, Alex Rønne Petersen, Damian Ziemba, Amaury SECHET

ソース std/bitmanip.d

string bitfields(T...)();
structsclassesunions の中にbitfields を作成できる。
bitfield は1つ以上のエントリで構成され、各エントリに固定数の
ビットが予約されている。
ビットで構成される。エントリーの型は、 の積分型または列挙型である。 bool、積分型、列挙型があり、任意に混在する。 bitfields に格納するのに最も効率的な型はbool である。 符号なし型、符号付き型の順である。
bitfieldbool 以外の各エントリーは、ユーザーが指定したビット数で表現される。 の非エントリーは、ユーザーが指定したビット数で表現される。このドメインを表現する最小値と最大値は この領域を表す最小値と最大値は、変数名の後に または をつけることで調べることができる。 の後に_min または_max を続ける。

制限 bitfield のビット数は8、16、32、64に制限されている、 32または64に制限される。パディングが必要な場合は、空の名前でエントリーを明示的に割り当てる。 空の名前で割り当てる必要がある。

実装の詳細 Bitfields は内部的に ubyte ushort uint ulong に格納される。 に格納される。ビットは、パラメータで指定された順序で埋められる、 最下位ビットから始まる。の保存に使われる 変数名は、bitfield の保存に使われる。 を連結して作成される。 _bf という接尾辞がつく。

Parameters:
れる。
T テンプレート・パラメーターのリストは、3つのチャンクに分割さ 項目に分割される。各チャンクは(この順序で)型、名前、数字からなる。 名前と番号で構成される。これらは一緒になって、 のエントリーを定義する。 bitfield : 与えられた型と名前の変数、 与えられた型と名前の変数であり、数値が示す数だけビットを保持することができる。
Returns:
bitfield を追加するためにmixin で使用できる文字列
である。
Examples:
8ビットの パックを作成する。 。 は最下位ビットから割り当てられる。 すなわち、 は、 ストレージの最下位2ビットを占める。 を占有する。 bitfield ubyte bitfields x bitfields
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
Examples:
1つの インスタンス化におけるすべてのビット長の合計はbitfield
、正確に8、16、32、64でなければならない。
のすべてのビット長の合計は、正確に8、16、32、または64でなければならない。パディングが必要な場合は つのビットフィールドを空の名前で割り当てる。
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
Examples:
列挙型も使用で
きる。
enum ABC { A, B, C }
struct EnumTest
{
    mixin(bitfields!(
              ABC, "x", 2,
              bool, "y", 1,
              ubyte, "z", 5));
}
enum auto taggedPointer(T : T*, string name, Ts...);
この文字列ミックスイン・ジェネレーターを使えば、構造体や クラスの内部にタグ付きポインターを作成することができる。
タグ付きポインターは、通常のポインターやクラス参照でゼロであることが分かっているビットを使って、余分な情報を格納
する。
例えば、整数へのポインターは4バイト・アラインでなければならないので、常にゼロであることが分かっているビットが2つある。 そこに2ビットの整数を格納することができる。
上の例では、構造体Aにタグ付きポインタを作成している。 uint* 型であり、2番目の引数で指定されたxという名前である。 引数で指定される。
続く引数は、bitfield'sと同じように動作する。ビットフィールドは ビットに収まらなければならない。
Examples:
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;
template taggedClassRef(T, string name, Ts...) if (is(T == class))
この文字列ミックスイン・ジェネレーターを使えば、構造体や クラスの内部にタグ付きクラス参照を作成することができる。
タグ付きクラス参照は、通常のクラス参照でゼロであることが分かっているビットを使って、余分な情報を格納
する。
例えば、整数へのポインターは4バイト・アラインでなければならないので、常にゼロであることが分かっているビットが2つある。 そこに2ビットの整数を格納することができる。
上の例では、構造体Aにオブジェクトへのタグ付き参照を作成している。 これは、taggedPointer と同じパラメー タを期待する。ただし、最初の引数はポインタ型ではなくクラス型でなければならない。
Examples:
struct A
{
    int a;
    mixin(taggedClassRef!(
        Object, "o",
        uint, "i", 2));
}
A obj;
obj.o = new Object();
obj.i = 3;
alias FloatRep = FloatingPointRepresentation!float.FloatingPointRepresentation;
の分数、指数、符号を別々に操作できる。 float を別々に操作できる。定義は以下の通りである:
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;
}
Examples:
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
Examples:
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
Examples:
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);
alias DoubleRep = FloatingPointRepresentation!double.FloatingPointRepresentation;
の分数、指数、符号を別々に操作できる。 double を別々に操作できる。定義は以下の通りである:
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;
}
Examples:
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
Examples:
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
Examples:
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);
Examples:
読書
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:
書く
DoubleRep x;
x.fraction = 1125899906842624;
x.exponent = 1025;
x.sign = true;
writeln(x.value); // -5.0
struct BitArray;
ビットの動的配列。の各ビットは個別に操作できる。 BitArrayの各ビットは個別に操作できる。 または標準的なビット演算子&,|,^,~,>>,<< によって操作できる。 他の有効なメンバ関数を使うこともできる。 BitArray's 次元( dim参照)に対して相対的に働く。 length.
Examples:
スライスとビットセット
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)));
Examples:
連結と追加
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]));
Examples:
ビット反転
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:
ビット配列の文字列フォーマット
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) 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"
pure nothrow this(in bool[] ba);
bool 配列からBitArray を作成し、bool の値を右から読み取る。 左から右に読み込まれた値が、BitArray の後続ビットに対応するように、 配列から を作成する。
Parameters:
bool[] ba bool 値のソース配列。
Examples:
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]));

// この配列では暗黙的にbool[]へのキャストが行われるため、これも機能する。
auto b = BitArray([0, 0, 1]);
writeln(b.length); // 3
assert(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); // 70
assert(a.bitsSet.equal(iota(0, 70)));
pure nothrow @nogc this(void[] v, size_t numbits);
ソース配列の生の内容からBitArray を作成する。ソース配列はコピーされない。 ソース配列はコピーされず、単にビットの配列として機能する。 として機能する。size_t 単位でデータを格納する。
つまり、 と異なる型の配列を渡す際には、特に注意が必要で
ある。
size_t の倍数でなければならないからである。 size_t.sizeof の倍数でなければならないからである。 がマッピングされるからである:
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);
}
size_t 、最下位ビットがこのユニットの開始ビットとなる。 最下位ビットはこのユニットの開始ビットであり、最上位ビットはこのユニットの最終ビットである。したがって、次のようになる、 従って、例えばintの配列を渡すと、プロセッサのエンディアンによって、 の配列が異なる結果となることがある。BitArray になる。
このコンストラクタはopCast の逆コンストラクタである。
Parameters:
。。
void[] v v.lengthsize_t.sizeof の複数でなければならない
size_t numbits ソース配列からマッピングされるビット数 作成されるBitArray の長さである。
Examples:
import std.algorithm.comparison : equal;

auto a = BitArray([1, 0, 0, 1, 1]);

// キャストの逆。
auto v = cast(void[]) a;
auto b = BitArray(v, a.length);

writeln(b.length); // 5
assert(b.bitsSet.equal([0, 3, 4]));

// aとbは基礎データを共有している。
a[0] = 0;
writeln(b[0]); // 0
writeln(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);
// 各ユニットの最下位ビットが、このユニットの開始ビットとなる。
assert(ba.bitsSet.equal([2, 3, sbits, sbits + 1]));
Examples:
// このコンストラクタのドキュメントからの例。
static immutable 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
}

// 配列の一部だけをマッピングする例。
import std.algorithm.comparison : equal;

auto bc = BitArray(source, sbits + 1);
assert(bc.bitsSet.equal([0, sbits]));
// 元配列は変更されていない。
writeln(source); // sourceData
const pure nothrow @nogc @property @safe size_t dim();
Returns:
次元、すなわちこのBitArray を支えるネイティブワードの数である。
厳密には、これはビットを格納する配列の長さである。 ceil(length / (size_t.sizeof * 8)) に等しい。 size_t と等しい。
const pure nothrow @nogc @property @safe size_t length();
Returns:
BitArray のビット数 .
pure nothrow @property @system size_t length(size_t newlen);
BitArray のビット数を設定する。 警告: 長さを長くすると、現在の基礎データの最終ワードのビットが上書きされる可能性がある。 のビットを上書きする可能性がある。 BitArrayオブジェクト間で共有されているかどうかに関係なく。 動的配列拡張セマンティクスに従わない。
const pure nothrow @nogc bool opIndex(size_t i);
BitArrayi'番目のビットを取得する。
Examples:
static void fun(const BitArray arr)
{
    auto x = arr[0];
    writeln(x); // 1
}
BitArray a;
a.length = 3;
a[0] = 1;
fun(a);
pure nothrow @nogc bool opIndexAssign(bool b, size_t i);
をセットする。 iBitArrayビットをセットする。
pure nothrow @nogc void opSliceAssign(bool val);
BitArray のすべての値を で指定された値に設定する。 val で指定された値に設定する。
Examples:
import std.algorithm.comparison : equal;

auto b = BitArray([1, 0, 1, 0, 1, 1]);

b[] = true;
// すべてのビットがセットされている
assert(b.bitsSet.equal([0, 1, 2, 3, 4, 5]));

b[] = false;
// どのビットもセットされていない
assert(b.bitsSet.empty);
pure nothrow @nogc void opSliceAssign(bool val, size_t start, size_t end);
BitArray のスライスのビットを設定する。 start のスライスのビットを設定する。 のスライスのビットを、val で指定された値で設定する。
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 void flip();
のすべてのビットを反転させる。BitArray
Examples:
import std.algorithm.comparison : equal;
import std.range : iota;

// ポジション0、2、4がセットされている
auto b = BitArray([1, 0, 1, 0, 1, 0]);
b.flip();
// 反転後、ポジション1、3、5がセットされる
assert(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 void flip(size_t pos);
で指定された1ビットを反転させる。 pos
Examples:
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
const pure nothrow @nogc scope @safe size_t count();
で指定したビットを反転させる。BitArray
Examples:
auto a = BitArray([0, 1, 1, 0, 0, 1, 1]);
writeln(a.count); // 4

BitArray b;
writeln(b.count); // 0

bool[200] boolArray;
boolArray[45 .. 130] = true;
auto c = BitArray(boolArray);
writeln(c.count); // 85
const pure nothrow @property BitArray dup();
BitArray 、その内容を複製する。
Examples:
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)
int opApply(scope int delegate(ref bool) dg);

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

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

const int opApply(scope int delegate(size_t, bool) dg);
foreach BitArray のループをサポートする。
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 BitArray reverse() return;
BitArray のビットを反転させる。
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 BitArray sort() return;
BitArray の要素をソートする。
Examples:
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
const pure nothrow @nogc bool opEquals(const ref BitArray a2);
BitArray の演算子 == と != をサポートする。
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); // e
assert(f != g);
const pure nothrow @nogc int opCmp(BitArray a2);
BitArray の比較演算子をサポート .
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); // e
assert(a <= e);
assert(a >= e);
assert(f <  g);
assert(g <= g);
const pure nothrow @nogc size_t toHash();
BitArray のハッシュをサポートする。
inout pure nothrow @nogc inout(void)[] opCast(T : const(void[]))();
void[] に変換する。
inout pure nothrow @nogc inout(size_t)[] opCast(T : const(size_t[]))();
size_t[] に変換する。
Examples:
import std.array : array;
import std.range : repeat, take;

// 300要素のビット配列
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
const pure nothrow BitArray opUnary(string op)()
if (op == "~");
BitArray の単項演算子 ~ をサポートする。
Examples:
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
const pure nothrow BitArray opBinary(string op)(const BitArray e2)
if (op == "-" || op == "&" || op == "|" || op == "^");
二項演算子ビット演算子のサポート forBitArray.
Examples:
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
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]); // 1
writeln(c[1]); // 0
writeln(c[2]); // 1
writeln(c[3]); // 1
writeln(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]); // 0
writeln(c[1]); // 0
writeln(c[2]); // 0
writeln(c[3]); // 1
writeln(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]); // 0
writeln(c[1]); // 0
writeln(c[2]); // 0
writeln(c[3]); // 0
writeln(c[4]); // 1
pure nothrow @nogc scope BitArray opOpAssign(string op)(const BitArray e2) return
if (op == "-" || op == "&" || op == "|" || op == "^");
BitArray の演算子 op= をサポートした。
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]); // 1
writeln(a[6]); // 0
writeln(a[7]); // 1
writeln(a[8]); // 0
writeln(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]); // 1
writeln(a[1]); // 0
writeln(a[2]); // 1
writeln(a[3]); // 0
writeln(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]); // 1
writeln(a[1]); // 0
writeln(a[2]); // 1
writeln(a[3]); // 1
writeln(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]); // 0
writeln(a[1]); // 0
writeln(a[2]); // 0
writeln(a[3]); // 1
writeln(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]); // 0
writeln(a[1]); // 0
writeln(a[2]); // 0
writeln(a[3]); // 0
writeln(a[4]); // 1
pure nothrow scope BitArray opOpAssign(string op)(bool b) return
if (op == "~");

pure nothrow scope BitArray opOpAssign(string op)(BitArray b) return
if (op == "~");
BitArray の演算子 ~= をサポートする。 警告:これは、現在の基礎データの最終ワード のビットを上書きする。 BitArrayオブジェクト間で共有されているかどうかに関係なく、これは現在のデータの最終ワードのビットを上書きする。 連結セマンティクスに従わない。
Examples:
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
Examples:
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
const pure nothrow BitArray opBinary(string op)(bool b)
if (op == "~");

const pure nothrow BitArray opBinaryRight(string op)(bool b)
if (op == "~");

const pure nothrow BitArray opBinary(string op)(BitArray b)
if (op == "~");
BitArray の二項演算子 ~ をサポートした。
Examples:
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
pure nothrow @nogc void opOpAssign(string op)(size_t nbits)
if (op == "<<");
演算子<<= のサポート。
配列のすべてのビットを、指定されたビット数だけ左にシフトする。 ビットだけ左にシフトする。 左端のビットは削除され、空いたビットを埋めるために末尾に0が追加される。 が付加される。
警告:次のワード境界までの最終ワードの未使用ビットは、この操作で上書きされる可能性がある。 の未使用ビットが上書きされる可能性がある。この操作では 配列の末尾を過ぎたビットを保持しようとはしない。
pure nothrow @nogc void opOpAssign(string op)(size_t nbits)
if (op == ">>");
演算子>>= をサポートする。
配列のすべてのビットを、指定されたビット数だけ右にシフトする。 ビットだけ右にシフトする。 右端のビットは削除され、0が後ろに挿入される。 が挿入される。
警告:次のワード境界までの最終ワードの未使用ビットは、この操作で上書きされる可能性がある。 の未使用ビットが上書きされる可能性がある。この操作では 配列の末尾を越えたビットを保持しようとしない。
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"
const void toString(W)(ref W sink, ref scope const FormatSpec!char fmt)
if (isOutputRange!(W, char));
この BitArray の文字列表現を返す。
2つのフォーマット指定子がサポートされている:
  • %s これはビットを配列として表示する。
  • %b ビットをアンダースコアで区切った8ビットのバイトパケットとして表示する
  • ビットをアンダースコアで区切って表示する。
    Parameters:
    W sink char出力範囲
    FormatSpec!char fmt A std.format.FormatSpecデータの表示方法を制御する を制御する。
    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"
    
    const nothrow @property auto bitsSet();
    セットされたビットのインデックスの遅延範囲を返す。
    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 T swapEndian(T)(const T val)
    if (isIntegral!T || isSomeChar!T || isBoolean!T);
    与えられた積分値または文字のエンディアンを入れ替える。
    Examples:
    writeln(42.swapEndian); // 704643072
    assert(42.swapEndian.swapEndian == 42); // 反射的
    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
    
    pure nothrow @nogc @safe auto nativeToBigEndian(T)(const T val)
    if (canSwapEndianness!T);
    与えられた値をネイティブエンディアンからビッグエンディアンに変換する。 ubyte[n] n は与えられた型のサイズである。
    ubyte[n] を返すことで、スワップされた値を誤って正規の値として使ってしまうことを防ぐことができる。 浮動小数点値の場合は必要である、 FPUはスワップされた浮動小数点値を台無しにしてしまうからだ。つまり を浮動小数点値として使用することはできない)。
    real はサポートされていない。 なぜなら、そのサイズは実装に依存するため、マシンによって異なる可能性があるからである。 他のマシンに転送しようとすると使えなくなる)。
    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)
    
    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)
    
    pure nothrow @nogc @safe T bigEndianToNative(T, size_t n)(ubyte[n] val)
    if (canSwapEndianness!T && (n == T.sizeof));
    与えられた値をビッグエンディアンからネイティブエンディアンに変換して返す。 それを返す。値はubyte[n]n はターゲット型のサイズである。 である。テンプレート引数としてターゲット型を与えなければならない、 なぜなら、同じサイズを持つ型が複数存在するため、引数の型だけでは戻り値を決定できないからである。 引数の型だけでは戻り値の型を決定できないからである。
    ubyte[n] 、スワップされた値を誤って正規の値として使ってしまうことを防ぐことができる。 浮動小数点値の場合は必要である、 FPUはスワップされた浮動小数点値を台無しにしてしまうからだ。つまり 実際に浮動小数点値としてスワップされた浮動小数点値を持つことはできない)。
    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 @safe auto nativeToLittleEndian(T)(const T val)
    if (canSwapEndianness!T);
    指定された値をネイティブエンディアンからリトルエンディアンに変換する。 ubyte[n] n は与えられた型のサイズである。
    ubyte[n] を返すことで、スワップされた値を誤って正規の値として使ってしまうことを防ぐことができる。 浮動小数点値の場合は必要である、 FPUはスワップされた浮動小数点値を台無しにしてしまうからだ。つまり を浮動小数点値として使用することはできない)。
    Examples:
    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)
    
    pure nothrow @nogc @safe T littleEndianToNative(T, size_t n)(ubyte[n] val)
    if (canSwapEndianness!T && (n == T.sizeof));
    与えられた値をリトルエンディアンからネイティブエンディアンに変換して返す。 それを返す。値はubyte[n]n はターゲット型のサイズである。 である。テンプレート引数としてターゲット型を与えなければならない、 なぜなら、同じサイズを持つ型が複数存在するため、引数の型だけでは戻り値を決定できないからである。 引数の型だけでは戻り値の型を決定できないからである。
    ubyte[n] 、スワップされた値を誤って正規の値として使ってしまうことを防ぐことができる。 浮動小数点値の場合は必要である、 FPUはスワップされた浮動小数点値を台無しにしてしまうからだ。つまり 実際に浮動小数点値としてスワップされた浮動小数点値を持つことはできない)。
    real はサポートされていない。 なぜなら、そのサイズは実装に依存するため、マシンによって異なる可能性があるからである。 他のマシンに転送しようとすると使えなくなる)。
    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)
    
    T peek(T, Endian endianness = Endian.bigEndian, R)(R range)
    if (canSwapEndianness!T && isForwardRange!R && is(ElementType!R : const(ubyte)));

    T peek(T, Endian endianness = Endian.bigEndian, R)(R range, size_t index)
    if (canSwapEndianness!T && isForwardRange!R && hasSlicing!R && is(ElementType!R : const(ubyte)));

    T peek(T, Endian endianness = Endian.bigEndian, R)(R range, size_t* index)
    if (canSwapEndianness!T && isForwardRange!R && hasSlicing!R && is(ElementType!R : const(ubyte)));
    ubytesの範囲を取り、最初のT.sizeof バイトを次のように変換する。 T.返される値は、指定されたエンディアンからネイティブエンディアンに変換される。 ネイティブエンディアンに変換される。範囲は消費されない。
    Parameters:
    T 最初のT.sizeof バイトを変換する積分型。
    endianness バイトが想定するエンディアン。
    R range 読み込む範囲。
    size_t index 読み込みを開始するインデックス(先頭からではなく から始めるのではなく)。indexがポインタの場合、読み込まれたバイトの後のindex に更新される。indexを使ったオーバーロードは、 が の場合のみ利用できる。 を持つオーバーロードは、hasSlicing!Rtrue の場合のみ利用可能である。
    Examples:
    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
    
    Examples:
    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
    
    T read(T, Endian endianness = Endian.bigEndian, R)(ref R range)
    if (canSwapEndianness!T && isInputRange!R && is(ElementType!R : const(ubyte)));
    ubytesの範囲を取り、最初のT.sizeof バイトを次のように変換する。 T.返される値は、指定されたエンディアンからネイティブエンディアンに変換される。 に変換される。読み込まれるT.sizeof バイトが、その範囲から消費される。 から消費される。
    Parameters:
    T 最初のT.sizeof バイトを変換する整数型。
    endianness バイトが想定するエンディアン。
    R range 読み込む範囲。
    Examples:
    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);
    
    void write(T, Endian endianness = Endian.bigEndian, R)(R range, const T value, size_t index)
    if (canSwapEndianness!T && isForwardRange!R && hasSlicing!R && is(ElementType!R : ubyte));

    void write(T, Endian endianness = Endian.bigEndian, R)(R range, const T value, size_t* index)
    if (canSwapEndianness!T && isForwardRange!R && hasSlicing!R && is(ElementType!R : ubyte));
    積分値を受け取り、それを与えられたエンディアンに変換し、 sのシーケンスとして与えられた範囲に書き込む。 インデックスから始まるT.sizeof ubyte s のシーケンスとして、与えられたubytes の範囲に書き込む。 hasSlicing!Rtrue でなければならない。
    Parameters:
    T 最初のT.sizeof バイトを変換する積分型。
    endianness バイトを書き込むエンディアン。
    R range 書き込む範囲。
    T value 書き込む値。
    size_t index 書き込みを開始するインデックス。indexがポインタの場合 は、読み込んだバイトの後のインデックスに更新される。
    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_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
    
    Examples:
    ブール
    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
    
    Examples:
    char(8ビット)
    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
    
    Examples:
    wchar (16bit - 2xubyte)
    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
    
    Examples:
    dchar (32bit - 4xubyte)
    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
    
    Examples:
    float (32bit - 4xubyte)
    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
    
    Examples:
    double (64bit - 8xubyte)
    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
    
    Examples:
    列挙型
    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
    
    Examples:
    列挙型 - 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_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
    
    Examples:
    列挙型 - 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_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
    
    Examples:
    列挙型 - 実数
    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)));
    
    void append(T, Endian endianness = Endian.bigEndian, R)(R range, const T value)
    if (canSwapEndianness!T && isOutputRange!(R, ubyte));
    積分値を受け取り、それを指定されたエンディアンに変換し、指定された範囲に追加する。 のシーケンスとして、与えられたubytes の範囲(put を使用)に追加する。 T.sizeof ubyte hasSlicing!Rtrue.
    Parameters:
    T 最初のT.sizeof バイトを変換する積分型。
    endianness バイトを書き込むエンディアン。
    R range 追加する範囲。
    T value 追加する値。
    Examples:
    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]
    
    Examples:
    ブール
    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]
    
    Examples:
    char wchar dchar
    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]
    
    Examples:
    float double
    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]
    
    Examples:
    列挙型
    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]
    
    Examples:
    列挙型 - bool
    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]
    
    Examples:
    列挙型 - float
    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]
    
    Examples:
    列挙型 - double
    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]
    
    Examples:
    列挙型 - 実数
    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)));
    
    pure nothrow @nogc auto bitsSet(T)(const T value)
    if (isIntegral!T);
    のセット・ビットのインデックスを反復する範囲。 value. インデックス0は最下位ビットに対応する。 符号付き整数の場合、最上位のインデックスは符号ビットに対応する。
    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]));