use std::fmt::Display;
#[test]
fn bit_bool_test() {
let x = OneBitBool::from(true);
let mut y = OneBitBool::from(true);
y.change_index(2, false);
println!("{} {}", x, y);
}
#[derive(Debug, PartialEq, Clone, Copy)]
pub struct OneBitBool {
bool: u8
}
impl OneBitBool {
pub fn from(bool: bool) -> Self {
if bool {
Self { bool: u8::MAX }
} else {
Self { bool: 0 }
}
}
pub fn empty() -> Self {
Self { bool: 0 }
}
pub fn get_index(&self, index: usize) -> bool {
self.bool & (1 << index) != 0
}
pub fn change_index(&mut self, index: usize, value: bool) {
if value {
self.bool |= 1 << index;
} else {
self.bool &= !(1 << index);
}
}
}
impl Display for OneBitBool {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{:b}", self.bool)
}
}