Duration to String enhanced with tests
Adds checks for if a bit in a u8 is set or clear
This commit is contained in:
@@ -8,12 +8,12 @@ impl NumberSystemConversion {
|
||||
/// ex: 0xff -> true, true, true, true, true, true, true, true
|
||||
/// 0xa0 -> true, false, true, false, false, false, false, false
|
||||
pub fn byte_to_bool(from: u8) -> [bool; 8] {
|
||||
let mut return_values = [false; 8];
|
||||
let mut result = [false; 8];
|
||||
for i in 0..8 {
|
||||
let new_value = from >> i & 0x1 == 1;
|
||||
return_values[i as usize] = new_value;
|
||||
// Extract the i-th bit and convert it to a boolean
|
||||
result[i] = ((from >> i) & 1) != 0;
|
||||
}
|
||||
return_values
|
||||
result
|
||||
}
|
||||
|
||||
/// bool_to_byte
|
||||
@@ -114,4 +114,19 @@ impl NumberSystemConversion {
|
||||
pub fn clear_low_bits(to_clear: u16) -> u16 {
|
||||
to_clear & 0xff00
|
||||
}
|
||||
|
||||
/// is_bit_set
|
||||
///
|
||||
/// Checks if a specified bit is set
|
||||
pub fn is_bit_set(to_check: u8, bit_to_check: u8) -> bool {
|
||||
(to_check >> bit_to_check) & 0x01 == 1
|
||||
}
|
||||
|
||||
/// is_bit_clear
|
||||
///
|
||||
/// Checks if a specified bit is clear
|
||||
/// LSB = 0 MSB = 7
|
||||
pub fn is_bit_clear(to_check: u8, bit_to_check: u8) -> bool {
|
||||
(to_check >> bit_to_check) & 0x01 != 1
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user