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
- pure nothrow @nogc @safe int
bsf
(uintv
);
pure nothrow @nogc @safe intbsf
(ulongv
); - 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);
- pure nothrow @nogc @safe int
bsr
(uintv
);
pure nothrow @nogc @safe intbsr
(ulongv
); - 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);
- pure nothrow @nogc @system int
bt
(scope const size_t*p
, size_tbitnum
); - 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);
- pure nothrow @nogc @system int
btc
(size_t*p
, size_tbitnum
); - Tests and complements the bit.
- pure nothrow @nogc @system int
btr
(size_t*p
, size_tbitnum
); - Tests and resets (sets to 0) the bit.
- pure nothrow @nogc @system int
bts
(size_t*p
, size_tbitnum
); - 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);
- struct
BitRange
; - Range over bit set. Each element is the bit number that is set.This is more efficient than testing each bit in a sparsely populated bit set. Note that the first bit in the bit set would be bit 0.Examples:
import core.stdc.stdlib : malloc, free; import core.stdc.string : memset; // initialize a bit array enum nBytes = (100 + BitRange.bitsPerWord - 1) / 8; size_t *bitArr = cast(size_t *)malloc(nBytes); scope(exit) free(bitArr); memset(bitArr, 0, nBytes); // set some bits bts(bitArr, 48); bts(bitArr, 24); bts(bitArr, 95); bts(bitArr, 78); enum sum = 48 + 24 + 95 + 78; // iterate size_t testSum; size_t nBits; foreach (b; BitRange(bitArr, 100)) { testSum += b; ++nBits; } assert(testSum == sum); assert(nBits == 4);
- enum ulong
bitsPerWord
; - Number of bits in each size_t
- pure nothrow @nogc @system this(const(size_t)*
bitarr
, size_tnumBits
); - Construct a BitRange.Parameters:
const(size_t)* bitarr
The array of bits to iterate over size_t numBits
The total number of valid bits in the given bit array - pure nothrow @nogc @safe size_t
front
();
const pure nothrow @nogc @safe boolempty
();
pure nothrow @nogc @system voidpopFront
(); - Range functions
- pure nothrow @nogc @safe ushort
byteswap
(ushortx
); - Swaps bytes in a 2 byte ushort.Parameters:
ushort x
value Returns:x
with bytes swappedExamples:assert(byteswap(cast(ushort)0xF234) == 0x34F2); static ushort xx = 0xF234; assert(byteswap(xx) == 0x34F2);
- pure nothrow @nogc @safe uint
bswap
(uintv
); - 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.Examples:
assert(bswap(0x01020304u) == 0x04030201u); static uint xx = 0x10203040u; assert(bswap(xx) == 0x40302010u);
- pure nothrow @nogc @safe ulong
bswap
(ulongv
); - Swaps bytes in an 8 byte ulong end-to-end, i.e. byte 0 becomes byte 7, byte 1 becomes byte 6, etc. This is meant to be recognized by the compiler as an intrinsic.Examples:
assert(bswap(0x01020304_05060708uL) == 0x08070605_04030201uL); static ulong xx = 0x10203040_50607080uL; assert(bswap(xx) == 0x80706050_40302010uL);
- nothrow @nogc @system ubyte
inp
(uintport_address
);
nothrow @nogc @system ushortinpw
(uintport_address
);
nothrow @nogc @system uintinpl
(uintport_address
); - Reads I/O port at port_address.
- nothrow @nogc @system ubyte
outp
(uintport_address
, ubytevalue
);
nothrow @nogc @system ushortoutpw
(uintport_address
, ushortvalue
);
nothrow @nogc @system uintoutpl
(uintport_address
, uintvalue
); - Writes and returns value to I/O port at port_address.
- pure nothrow @nogc @safe int
popcnt
(uintx
);
pure nothrow @nogc @safe intpopcnt
(ulongx
); - Calculates the number of set bits in an integer.
- pure nothrow @nogc @safe ushort
_popcnt
(ushortx
);
pure nothrow @nogc @safe int_popcnt
(uintx
);
pure nothrow @nogc @safe int_popcnt
(ulongx
); - Calculates the number of set bits in an integer using the X86 SSE4 POPCNT instruction. POPCNT is not available on all X86 CPUs.
- pure nothrow @nogc @safe uint
bitswap
(uintx
); - Reverses the order of bits in a 32-bit integer.
- pure nothrow @nogc @safe ulong
bitswap
(ulongx
); - Reverses the order of bits in a 64-bit integer.
- pure T
rol
(T)(const Tvalue
, const uintcount
)
if (__traits(isIntegral, T) && __traits(isUnsigned, T));
pure Tror
(T)(const Tvalue
, const uintcount
)
if (__traits(isIntegral, T) && __traits(isUnsigned, T));
pure Trol
(uint count, T)(const Tvalue
)
if (__traits(isIntegral, T) && __traits(isUnsigned, T));
pure Tror
(uint count, T)(const Tvalue
)
if (__traits(isIntegral, T) && __traits(isUnsigned, T)); - Bitwise rotate
value
left (rol
) or right (ror
) bycount
bit positions.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));
Copyright © 1999-2022 by the D Language Foundation | Page generated by
Ddoc on (no date time)