1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
use num_traits::Unsigned;
pub trait Bit: Unsigned {
fn set_bit(&mut self, pos: usize);
fn clear_bit(&mut self, pos: usize);
fn get_bit(&self, pos: usize) -> bool;
fn toggle_bit(&mut self, pos: usize);
fn set_all(&mut self, pos: usize);
fn is_power_of_two(&self) -> bool;
}
macro_rules! impl_bit {
($type:ty) => {
impl Bit for $type {
fn set_bit(&mut self, pos: usize) {
*self |= (1 << pos);
}
fn clear_bit(&mut self, pos: usize) {
*self &= !(1 << pos);
}
fn get_bit(&self, pos: usize) -> bool {
(self >> pos) % 2 != 0
}
fn toggle_bit(&mut self, pos: usize) {
*self ^= (1 << pos);
}
fn set_all(&mut self, pos: usize) {
*self = (1 << pos) - 1;
}
fn is_power_of_two(&self) -> bool {
*self > 0 && *self & (*self - 1) == 0
}
}
};
}
impl_bit!(u8);
impl_bit!(u16);
impl_bit!(u32);
impl_bit!(u64);
impl_bit!(u128);
impl_bit!(usize);
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_bit() {
let mut x = 0b10101010u8;
assert!(x.get_bit(1));
assert!(!x.get_bit(2));
x.set_bit(2);
assert!(x.get_bit(2));
x.clear_bit(2);
assert!(!x.get_bit(2));
x.toggle_bit(2);
assert!(x.get_bit(2));
x.set_all(5);
assert_eq!(x, 0b00011111);
assert!((x + 1).is_power_of_two());
}
}