#[derive(PartialEq)]
pub enum BitState {
Clear,
Set,
}
pub trait BitOps {
#[allow(missing_docs)]
fn set_bit(&mut self, pos: u8) -> Self;
#[allow(missing_docs)]
fn clear_bit(&mut self, pos: u8) -> Self;
#[allow(missing_docs)]
fn check_bit(&self, pos: u8) -> BitState;
}
impl BitOps for u8 {
fn set_bit(&mut self, pos: u8) -> Self {
assert!(pos <= 7, "bit offset larger than 7");
*self |= 1u8 << pos;
*self
}
fn clear_bit(&mut self, pos: u8) -> Self {
assert!(pos <= 7, "bit offset larger than 7");
*self &= !(1u8 << pos);
*self
}
fn check_bit(&self, pos: u8) -> BitState {
assert!(pos <= 7, "bit offset larger than 7");
match self.checked_shr(pos as u32).unwrap() & 1 == 1 {
true => BitState::Set,
false => BitState::Clear,
}
}
}