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

core.bitop

このモジュールには、ビットレベルの操作のコレクションが含まれている。
Authors:
Don Clugston, Sean Kelly, Walter Bright, Alex Rønne Petersen, Thomas Stuart Bockman

ソース core/bitop.d

pure nothrow @nogc @safe int bsf(uint v);

pure nothrow @nogc @safe int bsf(ulong v);
vのビットをビット0からスキャンし、最初にセットされたビットを探す。 を探す。
Returns:
最初にセットされたビットのビット番号を返す。 vが0の場合、戻り値は未定義である。
Examples:
assert(bsf(0x21) == 0);
assert(bsf(ulong.max << 39) == 39);
pure nothrow @nogc @safe int bsr(uint v);

pure nothrow @nogc @safe int bsr(ulong v);
vのビットを最上位ビットから最下位ビットまでスキャンする。 から最下位ビットまでを走査する。 最初にセットされたビットを探す。
Returns:
最初にセットされたビットのビット番号を返す。 vが0の場合、戻り値は未定義である。
Examples:
assert(bsr(0x21) == 5);
assert(bsr((ulong.max >> 15) - 1) == 48);
pure nothrow @nogc @system int bt(scope const size_t* p, size_t bitnum);
ビットをテストする。 (もはやイントリシクではない - コンパイラはボディ内のパターンを認識する) を認識する)。
Examples:
size_t[2] array;

array[0] = 2;
array[1] = 0x100;

assert(bt(array.ptr, 1));
assert(array[0] == 2);
assert(array[1] == 0x100);
pure nothrow @nogc @system int btc(size_t* p, size_t bitnum);
ビットをテストし、補完する。
pure nothrow @nogc @system int btr(size_t* p, size_t bitnum);
ビットをテストし、リセット(0にする)する。
pure nothrow @nogc @system int bts(size_t* p, size_t bitnum);
ビットをテストし、設定する。
Parameters:
size_t* p size_tsの配列へのNULLでないポインタ。
size_t bitnum p[0]のビット0から始まるビット番号、 ビット番号。式のようにビットを指定する:
p[index / (size_t.sizeof*8)] & (1 << (index & ((size_t.sizeof*8) - 1)))
Returns:
ビットがセットされていれば0以外の値、クリアされていれば0である。 である。
Examples:
size_t[2] array;

array[0] = 2;
array[1] = 0x100;

assert(btc(array.ptr, 35) == 0);
if (size_t.sizeof == 8)
{
    assert(array[0] == 0x8_0000_0002);
    assert(array[1] == 0x100);
}
else
{
    assert(array[0] == 2);
    assert(array[1] == 0x108);
}

assert(btc(array.ptr, 35));
assert(array[0] == 2);
assert(array[1] == 0x100);

assert(bts(array.ptr, 35) == 0);
if (size_t.sizeof == 8)
{
    assert(array[0] == 0x8_0000_0002);
    assert(array[1] == 0x100);
}
else
{
    assert(array[0] == 2);
    assert(array[1] == 0x108);
}

assert(btr(array.ptr, 35));
assert(array[0] == 2);
assert(array[1] == 0x100);
struct BitRange;
設定されているビットの範囲。各要素は設定されているビット番号である。
これは、ビットセットの各ビットをテストするよりも効率的である。 セットの各ビットをテストするよりも効率的である。ビットセットの最初のビットはビット0であることに注釈:。
Examples:
import core.stdc.stdlib : malloc, free;
import core.stdc.string : memset;

// ビット配列を初期化する
enum nBytes = (100 + BitRange.bitsPerWord - 1) / 8;
size_t *bitArr = cast(size_t *)malloc(nBytes);
scope(exit) free(bitArr);
memset(bitArr, 0, nBytes);

// いくつかのビットを設定する
bts(bitArr, 48);
bts(bitArr, 24);
bts(bitArr, 95);
bts(bitArr, 78);

enum sum = 48 + 24 + 95 + 78;

// 繰り返す
size_t testSum;
size_t nBits;
foreach (b; BitRange(bitArr, 100))
{
    testSum += b;
    ++nBits;
}

assert(testSum == sum);
assert(nBits == 4);
enum ulong bitsPerWord;
各size_tのビット数
pure nothrow @nogc @system this(const(size_t)* bitarr, size_t numBits);
BitRangeを構築する。
Parameters:
const(size_t)* bitarr 反復処理するビットの配列
size_t numBits 与えられたビット配列内の有効なビットの総数
pure nothrow @nogc @safe size_t front();

const pure nothrow @nogc @safe bool empty();

pure nothrow @nogc @system void popFront();
範囲関数".
pure nothrow @nogc @safe ushort byteswap(ushort x);
2バイトのushort内のバイトを入れ替える。
Parameters:
ushort x
Returns:
xバイトを入れ替えた
Examples:
assert(byteswap(cast(ushort)0xF234) == 0x34F2);
static ushort xx = 0xF234;
assert(byteswap(xx) == 0x34F2);
pure nothrow @nogc @safe uint bswap(uint v);
つまり、バイト0はバイト2になる。 バイト1がバイト2になり、バイト2がバイト1になり、バイト3がバイト0になる。 がバイト0になる。
Examples:
assert(bswap(0x01020304u) == 0x04030201u);
static uint xx = 0x10203040u;
assert(bswap(xx) == 0x40302010u);
pure nothrow @nogc @safe ulong bswap(ulong v);
つまり、バイト0はバイト6になる。 はバイト 7 に、バイト 1 はバイト 6 になる。 これは、組込み関数としてコンパイラに認識されることを意図している。
Examples:
assert(bswap(0x01020304_05060708uL) == 0x08070605_04030201uL);
static ulong xx = 0x10203040_50607080uL;
assert(bswap(xx) == 0x80706050_40302010uL);
nothrow @nogc @system ubyte inp(uint port_address);

nothrow @nogc @system ushort inpw(uint port_address);

nothrow @nogc @system uint inpl(uint port_address);
port_address の I/O ポートを読み込む。
nothrow @nogc @system ubyte outp(uint port_address, ubyte value);

nothrow @nogc @system ushort outpw(uint port_address, ushort value);

nothrow @nogc @system uint outpl(uint port_address, uint value);
port_addressのI/Oポートに値を書き込んで返す。
pure nothrow @nogc @safe int popcnt(uint x);

pure nothrow @nogc @safe int popcnt(ulong x);
整数のセットビット数を計算する
pure nothrow @nogc @safe ushort _popcnt(ushort x);

pure nothrow @nogc @safe int _popcnt(uint x);

pure nothrow @nogc @safe int _popcnt(ulong x);
X86 SSE4 POPCNT 命令を使用して、整数のセットビット数を計算する。 X86 SSE4 POPCNT 命令を使用する。 POPCNT は、すべての X86 CPU で使用できるわけではない。
pure nothrow @nogc @safe uint bitswap(uint x);
32 ビット整数内のビットの順序を逆にする。
pure nothrow @nogc @safe ulong bitswap(ulong x);
64 ビット整数のビットの順序を逆にする。
pure T rol(T)(const T value, const uint count)
if (__traits(isIntegral, T) && __traits(isUnsigned, T));

pure T ror(T)(const T value, const uint count)
if (__traits(isIntegral, T) && __traits(isUnsigned, T));

pure T rol(uint count, T)(const T value)
if (__traits(isIntegral, T) && __traits(isUnsigned, T));

pure T ror(uint count, T)(const T value)
if (__traits(isIntegral, T) && __traits(isUnsigned, T));
ビットごとの回転 value左 (rol)または右回転(ror)で回転する。 countビット単位で回転させる。
Examples:
ubyte a = 0b11110000U;
ulong b = ~1UL;

assert(rol(a, 1) == 0b11100001);
assert(ror(a, 1) == 0b01111000);
assert(rol(a, 3) == 0b10000111);
assert(ror(a, 3) == 0b00011110);

assert(rol(a, 0) == a);
assert(ror(a, 0) == a);

assert(rol(b, 63) == ~(1UL << 63));
assert(ror(b, 63) == ~2UL);

assert(rol!3(a) == 0b10000111);
assert(ror!3(a) == 0b00011110);

enum c = rol(uint(1), 0);
enum d = ror(uint(1), 0);
assert(c == uint(1));
assert(d == uint(1));