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.
| Operation | API | Meaning |
|---|---|---|
| Population count | Math.popcount(value) | Count set bits in the integer's width |
| Leading zero count | Math.clz(value) | Count zeros before the highest set bit |
| Trailing zero count | Math.ctz(value) | Count zeros after the lowest set bit |
| Byte reversal | Math.bswap(value) | Reverse byte order without changing width |
| Masked extraction | Bit.pext(value, mask) | Pack selected u64 bits into the low positions |
| Masked deposition | Bit.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:
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); // 0x78563412These 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.