Report a bug
If you spot a problem with this page, click here to create a Bugzilla 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.
core.bitop
This module contains a collection of bit-level operations.
License:
Authors:
Don Clugston, Sean Kelly, Walter Bright, Alex Rønne Petersen, Thomas Stuart Bockman
Source: core/bitop.d
- 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.Examples:
assert(bsf(0x21) == 0); assert(bsf(ulong.max << 39) == 39);
- 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.Examples:
assert(bsr(0x21) == 5); assert(bsr((ulong.max >> 15) - 1) == 48);
- Tests the bit. (No longer an intrisic - the compiler recognizes the patterns in the body.)Examples:
size_t[2] array; array[0] = 2; array[1] = 0x100; assert(bt(array.ptr, 1)); assert(array[0] == 2); assert(array[1] == 0x100);
- Tests and complements the bit.
- Tests and resets (sets to 0) the bit.
- Tests and sets the bit.Parameters:
size_t* p a non-NULL pointer to an array of size_ts. size_t bitnum 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.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);
- 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.
- Swaps bytes in an 8 byte ulong end-to-end, i.e. byte 0 becomes byte 7, byte 1 becomes byte 6, etc.
- Reads I/O port at port_address.
- Writes and returns value to I/O port at port_address.
- Calculates the number of set bits in an integer.
- Calculates the number of set bits in an integer using the X86 SSE4 POPCNT instruction. POPCNT is not available on all X86 CPUs.
- nothrow @nogc @safe ubyte volatileLoad(ubyte* ptr);
nothrow @nogc @safe ushort volatileLoad(ushort* ptr);
nothrow @nogc @safe uint volatileLoad(uint* ptr);
nothrow @nogc @safe ulong volatileLoad(ulong* ptr);
nothrow @nogc @safe void volatileStore(ubyte* ptr, ubyte value);
nothrow @nogc @safe void volatileStore(ushort* ptr, ushort value);
nothrow @nogc @safe void volatileStore(uint* ptr, uint value);
nothrow @nogc @safe void volatileStore(ulong* ptr, ulong value); - Read/write value from/to the memory location indicated by ptr.These functions are recognized by the compiler, and calls to them are guaranteed to not be removed (as dead assignment elimination or presumed to have no effect) or reordered in the same thread. These reordering guarantees are only made with regards to other operations done through these functions; the compiler is free to reorder regular loads/stores with regards to loads/stores done through these functions. This is useful when dealing with memory-mapped I/O (MMIO) where a store can have an effect other than just writing a value, or where sequential loads with no intervening stores can retrieve different values from the same location due to external stores to the location. These functions will, when possible, do the load/store as a single operation. In general, this is possible when the size of the operation is less than or equal to (void*).sizeof, although some targets may support larger operations. If the load/store cannot be done as a single operation, multiple smaller operations will be used. These are not to be conflated with atomic operations. They do not guarantee any atomicity. This may be provided by coincidence as a result of the instructions used on the target, but this should not be relied on for portable programs. Further, no memory fences are implied by these functions. They should not be used for communication between threads. They may be used to guarantee a write or read cycle occurs at a specified address.
- Reverses the order of bits in a 32-bit integer.
- Reverses the order of bits in a 64-bit integer.
- pure T rol(T)(in T value, in uint count)
if (__traits(isIntegral, T) && __traits(isUnsigned, T));
pure T ror(T)(in T value, in uint count)
if (__traits(isIntegral, T) && __traits(isUnsigned, T));
pure T rol(uint count, T)(in T value)
if (__traits(isIntegral, T) && __traits(isUnsigned, T));
pure T ror(uint count, T)(in T value)
if (__traits(isIntegral, T) && __traits(isUnsigned, T)); -
Examples:
ubyte a = 0b10101010U; ulong b = ulong.max; assert(rol(a, 1) == 0b01010101); assert(ror(a, 1) == 0b01010101); assert(rol(a, 3) == 0b01010101); assert(ror(a, 3) == 0b01010101); assert(rol(a, 0) == a); assert(ror(a, 0) == a); assert(rol(b, 63) == ulong.max); assert(ror(b, 63) == ulong.max); assert(rol!3(a) == 0b01010101); assert(ror!3(a) == 0b01010101);
Copyright Don Clugston 2005 - 2013.
| Page generated by
Ddoc on (no date time)