1//! Utilities to generate bitmasks.
2
3#![doc(hidden)]
4
5/// Generate a bitwise mask for the lower `n` bits.
6///
7/// # Examples
8///
9/// ```rust
10/// # use minimal_lexical::mask::lower_n_mask;
11/// # pub fn main() {
12/// assert_eq!(lower_n_mask(2), 0b11);
13/// # }
14/// ```
15#[inline]
16pub fn lower_n_mask(n: u64) -> u64 {
17 debug_assert!(n <= 64, "lower_n_mask() overflow in shl.");
18
19 match n == 64 {
20 // u64::MAX for older Rustc versions.
21 true => 0xffff_ffff_ffff_ffff,
22 false => (1 << n) - 1,
23 }
24}
25
26/// Calculate the halfway point for the lower `n` bits.
27///
28/// # Examples
29///
30/// ```rust
31/// # use minimal_lexical::mask::lower_n_halfway;
32/// # pub fn main() {
33/// assert_eq!(lower_n_halfway(2), 0b10);
34/// # }
35/// ```
36#[inline]
37pub fn lower_n_halfway(n: u64) -> u64 {
38 debug_assert!(n <= 64, "lower_n_halfway() overflow in shl.");
39
40 match n == 0 {
41 true => 0,
42 false => nth_bit(n - 1),
43 }
44}
45
46/// Calculate a scalar factor of 2 above the halfway point.
47///
48/// # Examples
49///
50/// ```rust
51/// # use minimal_lexical::mask::nth_bit;
52/// # pub fn main() {
53/// assert_eq!(nth_bit(2), 0b100);
54/// # }
55/// ```
56#[inline]
57pub fn nth_bit(n: u64) -> u64 {
58 debug_assert!(n < 64, "nth_bit() overflow in shl.");
59 1 << n
60}
61