1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
use word::{Word, ToWord, UnsignedWord};

/// Clears all bits of `x` at position <= `bit`.
///
/// # Panics
///
/// If `bit >= bit_size()`.
///
/// # Examples
///
/// ```
/// use bitwise::word::*;
///
/// let n = 0b1111_0010u8;
/// let s = 0b1100_0000u8;
/// assert_eq!(n.clear_bits_leq(5u8), s);
/// assert_eq!(clear_bits_leq(n, 5u8), s);
/// ```
#[inline]
pub fn clear_bits_leq<T: Word, U: UnsignedWord>(x: T, bit: U) -> T {
    debug_assert!(T::bit_size() > bit.to());
    x & !((T::one() << (T::one() + bit.to())) - T::one())
}

/// Method version of [`clear_bits_leq`](fn.clear_bits_leq.html).
pub trait ClearBitsLeq {
    #[inline]
    fn clear_bits_leq<U: UnsignedWord>(self, n: U) -> Self;
}

impl<T: Word> ClearBitsLeq for T {
    #[inline]
    fn clear_bits_leq<U: UnsignedWord>(self, n: U) -> Self {
        clear_bits_leq(self, n)
    }
}