Skip to content
Systems programming for what’s next.
— Vex

Bit operations ​

Bit operations are compiler builtins; there is no bit package to import. Use Math.* for integer counts and byte reversal, and Bit.* for masked extraction and deposition.

OperationAPIMeaning
Population countMath.popcount(value)Count set bits in the integer's width
Leading zero countMath.clz(value)Count zeros before the highest set bit
Trailing zero countMath.ctz(value)Count zeros after the lowest set bit
Byte reversalMath.bswap(value)Reverse byte order without changing width
Masked extractionBit.pext(value, mask)Pack selected u64 bits into the low positions
Masked depositionBit.pdep(value, mask)Spread low u64 bits into the mask's set positions

Integer width matters ​

The Math integer operations preserve the operand's integer type. An explicit width makes a wire-format or bitset calculation independent of literal inference:

vex
let flags: u32 = 0b1011;
let count: u32 = Math.popcount(flags); // 3
let leading: u32 = Math.clz(4 as u32); // 29
let trailing: u64 = Math.ctz(8 as u64); // 3
let reversed: u32 = Math.bswap(0x12345678 as u32); // 0x78563412

These expressions illustrate the builtin signatures; they are not a complete program. Bit.popcnt32, Bit.lzcnt32, Bit.bswap32, and Bit.rotl64 are not members of the current builtin namespace.

Choosing a masked operation ​

Use pext when a sparse flag layout needs a compact index. Use pdep to place that compact representation back into a known layout. Bits outside the mask are discarded; the operation is reversible only within the selected positions. The Bit namespace guide includes a complete program, worked binary examples, and round-trip identities.

Execution and containers ​

The compiler chooses instructions or software lowering from the target's capabilities. A builtin name does not promise one instruction or a particular latency. The current Bit.pext and Bit.pdep signatures are scalar (u64, u64) -> u64; they do not imply Tensor or Span overloads.

See Math integer operations for the width-preserving operations and SIMD for container execution rules.

Released under the MIT License.