std/core/bits
std/bits — integer bit-twiddling primitives.
Population count + leading/trailing-zero count + (when applicable)
byte swap route through the matching cttz_* / ctlz_* /popcnt_* compiler builtin. Each lowers to one LLVM intrinsic
(@llvm.cttz / @llvm.ctlz / @llvm.ctpop), which the backend
further compiles to a single native instruction (tzcnt /lzcnt / popcnt on x86 BMI1+POPCNT). Routing through the builtin
reclaims the intended one-instruction lowering instead of a longtestb-chain expansion in every probe.
All functions take signed i32 / i64 but treat the operand as a raw bit
pattern. leadingZerosN(0) = N and trailingZerosN(0) = N by convention,
which matches LLVM's is_zero_poison = false.
Free functions
| Type | Method and description |
|---|---|
| popcount32(x : i32) : i32 Count of set bits in the 32-bit operand. |
| popcount64(x : i64) : i32 Count of set bits in the 64-bit operand. |
| leadingZeros32(x : i32) : i32 Number of leading zero bits in the 32-bit operand. |
| leadingZeros64(x : i64) : i32 Number of leading zero bits in the 64-bit operand. |
| trailingZeros32(x : i32) : i32 Number of trailing zero bits in the 32-bit operand. |
| trailingZeros64(x : i64) : i32 Number of trailing zero bits in the 64-bit operand. |
| byteSwap32(x : i32) : i32 Reverse the byte order of a 32-bit value. |
| byteSwap64(x : i64) : i64 Reverse the byte order of a 64-bit value. |
| rotateLeft32(x : i32, n : i32) : i32 Rotate the 32-bit value left by n bits. |
| rotateLeft64(x : i64, n : i32) : i64 Rotate the 64-bit value left by n bits. |
| rotateRight32(x : i32, n : i32) : i32 Rotate the 32-bit value right by n bits. |
| rotateRight64(x : i64, n : i32) : i64 Rotate the 64-bit value right by n bits. |
| bitReverse32(x : i32) : i32 Reverse the order of all 32 bits. |
| bitReverse64(x : i64) : i64 Reverse the order of all 64 bits. |
| isPowerOfTwo32(x : i32) : bool True when x is a positive power of two. |
| isPowerOfTwo64(x : i64) : bool True when x is a positive power of two. |
| nextPowerOfTwo32(x : i32) : i32 Smallest power of two ≥ x. nextPowerOfTwo*(0) = 1 byconvention. Saturates at the type's max power of two (no overflow promotion to a wider type). |
| nextPowerOfTwo64(x : i64) : i64 64-bit counterpart of nextPowerOfTwo32. |
Method detail
#popcount32
Count of set bits in the 32-bit operand.
x raw 32-bit pattern whose set bits are counted (sign ignored)#popcount64
Count of set bits in the 64-bit operand.
x raw 64-bit pattern whose set bits are counted (sign ignored)#leadingZeros32
Number of leading zero bits in the 32-bit operand.
x raw 32-bit pattern scanned from the MSB; 0 yields 32#leadingZeros64
Number of leading zero bits in the 64-bit operand.
x raw 64-bit pattern scanned from the MSB; 0 yields 64#trailingZeros32
Number of trailing zero bits in the 32-bit operand.
x raw 32-bit pattern scanned from the LSB; 0 yields 32#trailingZeros64
Number of trailing zero bits in the 64-bit operand.
x raw 64-bit pattern scanned from the LSB; 0 yields 64#byteSwap32
Reverse the byte order of a 32-bit value.
x 4-byte pattern whose bytes are reversed (b0↔b3, b1↔b2)#byteSwap64
Reverse the byte order of a 64-bit value.
x 8-byte pattern whose bytes are reversed (b0↔b7 … b3↔b4)#rotateLeft32
Rotate the 32-bit value left by n bits.
x 32-bit pattern to rotaten shift amount, taken modulo 32 (n & 31)#rotateLeft64
Rotate the 64-bit value left by n bits.
x 64-bit pattern to rotaten shift amount, taken modulo 64 (n & 63)#rotateRight32
Rotate the 32-bit value right by n bits.
x 32-bit pattern to rotaten shift amount, taken modulo 32 (n & 31)#rotateRight64
Rotate the 64-bit value right by n bits.
x 64-bit pattern to rotaten shift amount, taken modulo 64 (n & 63)#bitReverse32
Reverse the order of all 32 bits.
x 32-bit pattern whose bit i moves to position 31 - i#bitReverse64
Reverse the order of all 64 bits.
x 64-bit pattern whose bit i moves to position 63 - i#isPowerOfTwo32
True when x is a positive power of two.
x 32-bit value tested via x > 0 && (x & (x-1)) == 0#isPowerOfTwo64
True when x is a positive power of two.
x 64-bit value tested via x > 0 && (x & (x-1)) == 0#nextPowerOfTwo32
Smallest power of two ≥ x. nextPowerOfTwo*(0) = 1 by
convention. Saturates at the type's max power of two (no overflow
promotion to a wider type).
x 32-bit value; x <= 1 returns 1, else rounds up to a power of two#nextPowerOfTwo64
64-bit counterpart of nextPowerOfTwo32.
x 64-bit value; x <= 1 returns 1, else rounds up to a power of two