Add num_true_bits to bitfield
This commit is contained in:
parent
eb2ac990d7
commit
f48fb29b03
@ -26,7 +26,7 @@ impl BooleanBitfield {
|
||||
}
|
||||
}
|
||||
|
||||
// Output the bitfield as a big-endian vec of u8
|
||||
// Output the bitfield as a big-endian vec of u8.
|
||||
pub fn to_be_vec(&self) -> Vec<u8> {
|
||||
let mut o = self.vec.clone();
|
||||
o.reverse();
|
||||
@ -60,6 +60,19 @@ impl BooleanBitfield {
|
||||
false => self.vec[byte] = self.vec[byte] & !(1 << (bit as u8))
|
||||
}
|
||||
}
|
||||
|
||||
// Return the total number of bits set to true.
|
||||
pub fn num_true_bits(&mut self) -> u64 {
|
||||
let mut count: u64 = 0;
|
||||
for byte in &self.vec {
|
||||
for bit in 0..8 {
|
||||
if byte & (1 << (bit as u8)) != 0 {
|
||||
count += 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
count
|
||||
}
|
||||
}
|
||||
|
||||
impl Encodable for BooleanBitfield {
|
||||
@ -138,4 +151,19 @@ mod tests {
|
||||
assert_eq!(e[1], 128);
|
||||
assert_eq!(e[2], 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_bitfield_num_true_bits() {
|
||||
let mut b = BooleanBitfield::new();
|
||||
assert_eq!(b.num_true_bits(), 0);
|
||||
b.set_bit(&15, &true);
|
||||
assert_eq!(b.num_true_bits(), 1);
|
||||
b.set_bit(&15, &false);
|
||||
assert_eq!(b.num_true_bits(), 0);
|
||||
b.set_bit(&0, &true);
|
||||
b.set_bit(&7, &true);
|
||||
b.set_bit(&8, &true);
|
||||
b.set_bit(&1337, &true);
|
||||
assert_eq!(b.num_true_bits(), 4);
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user