Report a bug
If you spot a problem with this page, click here to create a GitHub 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 using a local clone.

mir.bitop

This module contains a collection of bit-level operations.
Authors:
Ilia Ki, Phobos & LDC Authors (original Phobos unittests, docs, conventions).
enum uint bitElemShift(T : ubyte);

enum uint bitElemShift(T : byte);

enum uint bitElemShift(T : ushort);

enum uint bitElemShift(T : short);

enum uint bitElemShift(T : uint);

enum uint bitElemShift(T : int);

enum uint bitElemShift(T : ulong);

enum uint bitElemShift(T : long);
Right shift vallue for bit index to get element's index (5 for uint).
enum uint bitShiftMask(T : ubyte);

enum uint bitShiftMask(T : byte);

enum uint bitShiftMask(T : ushort);

enum uint bitShiftMask(T : short);

enum uint bitShiftMask(T : uint);

enum uint bitShiftMask(T : int);

enum uint bitShiftMask(T : ulong);

enum uint bitShiftMask(T : long);
Bit mask for bit index to get element's bit shift (31 for uint).
T nTrailingBitsToCount(T)(in T value, in T popcnt)
if (__traits(isUnsigned, T));
Examples:
assert(nTrailingBitsToCount(0xF0u, 3u) == 7);
assert(nTrailingBitsToCount(0xE00u, 3u) == 12);

foreach(uint i; 1 .. 32)
    assert(nTrailingBitsToCount(uint.max, i) == i);
T nLeadingBitsToCount(T)(in T value, in T popcnt)
if (__traits(isUnsigned, T));
Examples:
assert(nLeadingBitsToCount(0xF0u, 3u) == 32 - 5);
assert(nLeadingBitsToCount(0x700u, 3u) == 32 - 8);

foreach(uint i; 1 .. 32)
    assert(nLeadingBitsToCount(uint.max, i) == i);
auto bt(Field, T = typeof(Field.init[size_t.init]))(auto ref Field p, size_t bitnum)
if (__traits(isUnsigned, T));
Tests the bit.
Returns:
A non-zero value if the bit was set, and a zero if it was clear.
Examples:
size_t[2] array;

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

assert(bt(array.ptr, 1));
assert(array[0] == 2);
assert(array[1] == 0x100);
auto bta(Field, T = typeof(Field.init[size_t.init]))(auto ref Field p, size_t bitnum, bool value)
if (__traits(isUnsigned, T));
Tests and assign the bit.
Returns:
A non-zero value if the bit was set, and a zero if it was clear.
auto btc(Field, T = typeof(Field.init[size_t.init]))(auto ref Field p, size_t bitnum)
if (__traits(isUnsigned, T));
Tests and complements the bit.
Returns:
A non-zero value if the bit was set, and a zero if it was clear.
auto btr(Field, T = typeof(Field.init[size_t.init]))(auto ref Field p, size_t bitnum)
if (__traits(isUnsigned, T));
Tests and resets (sets to 0) the bit.
Returns:
A non-zero value if the bit was set, and a zero if it was clear.
auto bts(Field, T = typeof(Field.init[size_t.init]))(auto ref Field p, size_t bitnum)
if (__traits(isUnsigned, T));
Tests and sets the bit.
Parameters:
Field p a non-NULL field / pointer to an array of unsigned integers.
size_t bitnum a bit number, starting with bit 0 of p[0], and progressing. It addresses bits like the expression:
p[index / (T.sizeof*8)] & (1 << (index & ((T.sizeof*8) - 1)))
Returns:
A non-zero value if the bit was set, and a zero if it was clear.
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);
T ctpop(T)(in T src)
if (__traits(isUnsigned, T));
The 'ctpop' family of intrinsics counts the number of bits set in a value.
T ctlz(T)(in T src)
if (__traits(isUnsigned, T));
The 'ctlz' family of intrinsic functions counts the number of leading zeros in a variable. Result is undefined if the argument is zero.
Examples:
assert(ctlz(cast(ubyte) 0b0011_1111) == 2);
assert(ctlz(cast(ushort) 0b0000_0001_1111_1111) == 7);
T ctlzp(T)(in T src)
if (__traits(isUnsigned, T));
The 'ctlzp' family of intrinsic functions counts the number of leading zeros in a variable. Result is properly defined if the argument is zero.
Examples:
assert(ctlzp(cast(ubyte) 0b0000_0000) == 8);
assert(ctlzp(cast(ubyte) 0b0011_1111) == 2);
assert(ctlzp(cast(ushort) 0b0000_0001_1111_1111) == 7);
assert(ctlzp(cast(ushort) 0) == 16);
assert(ctlzp(cast(ulong) 0) == 64);
T cttz(T)(in T src)
if (__traits(isUnsigned, T));
The 'cttz' family of intrinsic functions counts the number of trailing zeros. Result is undefined if the argument is zero.
Examples:
assert(cttzp(cast(ubyte) 0b11111100) == 2);
assert(cttzp(cast(ushort) 0b1111111110000000) == 7);
T cttzp(T)(in T src)
if (__traits(isUnsigned, T));
The 'cttz' family of intrinsic functions counts the number of trailing zeros. Result is properly defined if the argument is zero.
Examples:
assert(cttzp(cast(ubyte) 0b0000_0000) == 8);
assert(cttzp(cast(ubyte) 0b11111100) == 2);
assert(cttzp(cast(ushort) 0b1111111110000000) == 7);
assert(cttzp(cast(ushort) 0) == 16);
assert(cttzp(cast(ulong) 0) == 64);