-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.rs
39 lines (37 loc) · 855 Bytes
/
utils.rs
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
//! Utilities
/// Get bit
/// ```
/// # use inv8080rs::utils::get_bit;
/// let data: u8 = 0b10100101;
///
/// assert!(get_bit(data, 0));
/// assert!(!get_bit(data, 1));
/// assert!(get_bit(data, 2));
/// assert!(!get_bit(data, 3));
/// assert!(!get_bit(data, 4));
/// assert!(get_bit(data, 5));
/// assert!(!get_bit(data, 6));
/// assert!(get_bit(data, 7));
/// ```
pub fn get_bit(val: u8, n: u8) -> bool {
(val & (1 << n)) != 0
}
/// Set bit
/// ```
/// # use inv8080rs::utils::{set_bit, get_bit};
/// let mut data: u8 = 0;
///
/// for i in 0..8 {
/// set_bit(&mut data, i, true);
/// assert!(get_bit(data, i));
/// set_bit(&mut data, i, false);
/// assert!(!get_bit(data, i));
/// }
/// ```
pub fn set_bit(value: &mut u8, n: u8, val: bool) {
if val {
*value |= 1 << n;
} else {
*value &= !(1 << n);
}
}