Skip to main content

Module bits

Module bits 

Source
Expand description

Bit-manipulation operations beyond PrimInt: unbounded and funnel shifts, exact (lossless) shifts, bit isolation/indexing, bit width and PDEP/PEXT-style bit deposit/extract, mirroring the corresponding inherent methods on the primitive integer types.

Stability in std (as of nightly 2026): unbounded_shl/unbounded_shr are stable since 1.87; highest_one/lowest_one/isolate_highest_one/ isolate_lowest_one/bit_width since 1.98; funnel shifts, exact shifts and deposit_bits/extract_bits are still nightly-only. Everything newer than the crate’s MSRV is hand-rolled with the same semantics.

CT tiers: IsolateHighestOne/IsolateLowestOne, BitWidth and the funnel/unbounded shifts (under the public-parameter convention for shift amounts) are Tier A. DepositBits/ExtractBits are branchless on the operand but this portable fallback’s loop count is popcount(mask), so they are Tier A only when the mask is public (it is the analogue of a shift amount); for a secret mask they are Tier C. HighestOne/LowestOne and ShlExact/ShrExact are Tier B (Option returns).

Traits§

BitWidth
Computes the minimal number of bits required to represent an unsigned value.
DepositBits
Scatters bits through a mask (the PDEP operation).
ExtractBits
Gathers bits through a mask (the PEXT operation).
FunnelShl
Performs a funnel left shift on a double-width value formed from two words.
FunnelShr
Performs a funnel right shift on a double-width value formed from two words.
HighestOne
Finds the index of the highest one-bit.
IsolateHighestOne
Isolates the highest one-bit, branchlessly.
IsolateLowestOne
Isolates the lowest one-bit, branchlessly.
LowestOne
Finds the index of the lowest one-bit.
ShlExact
Performs a lossless (exactly reversible) left shift.
ShrExact
Performs a lossless (exactly reversible) right shift.
UnboundedShl
Performs a left shift that never panics, returning 0 for large shifts.
UnboundedShr
Performs a right shift that never panics, shifting in zero or sign bits for large shift amounts.