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};

/// Test the `bit` of `x`.
///
/// # Panics
///
/// If `bit >= bit_size()`.
///
/// # Examples
///
/// ```
/// use bitwise::word::*;
///
/// let n = 0b1011_0010u8;
/// assert_eq!(test_bit(n, 7u8), true);
/// assert_eq!(n.test_bit(6u8), false);
/// assert_eq!(n.test_bit(5u8), true);
/// ```
#[inline]
pub fn test_bit<T: Word, U: UnsignedWord>(x: T, bit: U) -> bool {
    debug_assert!(T::bit_size() > bit.to());
    x & (T::one() << bit.to()) != T::zero()
}

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

impl<T: Word> TestBit for T {
    #[inline]
    fn test_bit<U: UnsignedWord>(self, n: U) -> bool {
        test_bit(self, n)
    }
}