Expand description
Bit manipulation utilities and a tiny fixed-size bitset.
The free-standing functions cover the operations that show up over and over in competitive programming:
popcount— number of set bitstrailing_zeros/trailing_ones— index of the lowest set / unset bithighest_bit/lowest_bit— read the most / least significant set bitis_power_of_twonext_power_of_two/log2_floorsubsets— iterate over all2^knon-empty subsets of ak-bit mask
Bitset wraps Vec<u64> and exposes the operations you’d expect:
get, set, reset, flip, plus count_ones over the whole set.
All inputs are u64 unless documented otherwise.
Structs§
- Bitset
- Fix-sized bitset backed by
Vec<u64>.
Functions§
- highest_
bit - Mask with the highest set bit. Returns 0 if
x == 0. - is_
power_ of_ two - True iff
xis a (positive) power of two. - log2_
floor - Floor of the base-2 logarithm of
x. Panics ifx == 0. - lowest_
bit - Mask with the lowest set bit. Returns 0 if
x == 0. - next_
power_ of_ two - Smallest power of two ≥
x. Returns 1 forx == 0. - popcount
- Number of set bits in
x. - subsets
- Iterate over all
2^knon-empty subsets of ak-bit mask. - trailing_
ones - Index of the lowest unset bit of
!x. Returns 64 ifxis all ones. - trailing_
zeros - Index of the lowest set bit. Returns 64 if
x == 0.