vapfs_ufs/src/bitmap.rs

31 lines
No EOL
995 B
Rust

pub const SET: bool = true;
pub const UNSET: bool = false;
/// returns the bit index of the first bit that is, if value is true, set, or if value is false, unset
pub fn find_first_bit_equal_to(bitmap: &[u8], value: bool) -> Option<usize> {
for (byte_index, byte) in bitmap.iter().enumerate() {
for bit_index in 0..8 {
let bit = (byte >> bit_index) & 1;
if bit == (value as u8) {
return Some(byte_index * 8 + bit_index);
}
}
}
None
}
/// sets the bit at the given index to the given value
pub fn set_bit(bitmap: &mut [u8], index: usize, value: bool) {
let byte_index = index / 8;
let bit_index = index % 8;
let bit = (value as u8) << bit_index;
bitmap[byte_index] |= bit;
}
/// returns the bit at the given index
pub fn get_bit(bitmap: &[u8], index: usize) -> bool {
let byte_index = index / 8;
let bit_index = index % 8;
let bit = (bitmap[byte_index] >> bit_index) & 1;
bit == 1
}