D Programming Language 2.0

Last update Tue Jan 1 10:14:39 2013

# core.bitop

This module contains a collection of bit-level operations.

Authors:
Don Clugston, Sean Kelly, Walter Bright, Alex RĂ¸nne Petersen

Source:
core/bitop.d

pure nothrow @safe int bsf(size_t v);
Scans the bits in v starting with bit 0, looking for the first set bit.

Returns:
The bit number of the first bit set. The return value is undefined if v is zero.

Example:
``` import core.bitop;

int main()
{
assert(bsf(0x21) == 0);
return 0;
}
```

pure nothrow @safe int bsr(size_t v);
Scans the bits in v from the most significant bit to the least significant bit, looking for the first set bit.

Returns:
The bit number of the first bit set. The return value is undefined if v is zero.

Example:
``` import core.bitop;

int main()
{
assert(bsr(0x21) == 5);
return 0;
}
```

pure nothrow @safe int bt(in uint* p, size_t bitnum);
Tests the bit.

pure nothrow @safe int btc(uint* p, size_t bitnum);
Tests and complements the bit.

pure nothrow @safe int btr(uint* p, size_t bitnum);
Tests and resets (sets to 0) the bit.

pure nothrow @safe int bts(uint* p, size_t bitnum);
Tests and sets the bit.

Parameters:
 uint* p a non-NULL pointer to an array of size_ts. index a bit number, starting with bit 0 of p[0], and progressing. It addresses bits like the expression: ```p[index / (size_t.sizeof*8)] & (1 << (index & ((size_t.sizeof*8) - 1))) ```

Returns:
A non-zero value if the bit was set, and a zero if it was clear.

Example:
```import std.stdio;
import core.bitop;

int main()
{
size_t array[2];

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

assert(btc(array, 35) == 0);
assert(array[0] == 2);
assert(array[1] == 0x108);

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

assert(bts(array, 35) == 0);
assert(array[0] == 2);
assert(array[1] == 0x108);

assert(btr(array, 35) == -1);
assert(array[0] == 2);
assert(array[1] == 0x100);

assert(bt(array, 1) == -1);
assert(array[0] == 2);
assert(array[1] == 0x100);

return 0;
}
```

pure nothrow @safe uint bswap(uint v);
Swaps bytes in a 4 byte uint end-to-end, i.e. byte 0 becomes byte 3, byte 1 becomes byte 2, byte 2 becomes byte 1, byte 3 becomes byte 0.