Support bit operations
This commit is contained in:
parent
e79df423b0
commit
090d0d09a1
|
@ -6,3 +6,4 @@ edition = "2021"
|
|||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
paste = "*"
|
|
@ -0,0 +1,30 @@
|
|||
pub struct BitRange {
|
||||
pub start: usize,
|
||||
pub len: usize,
|
||||
}
|
||||
|
||||
impl BitRange {
|
||||
pub const fn from_to(start: usize, end: usize) -> BitRange {
|
||||
BitRange{start, len: (end - start + 1)}
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! create_bit_functions {
|
||||
($type_val:ty) => {
|
||||
paste::item! {
|
||||
fn [< get_mask_ $type_val >](range: &BitRange) -> $type_val {
|
||||
(1 << range.len) - 1
|
||||
}
|
||||
|
||||
pub fn [< clear_value_ $type_val >](dst: $type_val, range: &BitRange) -> $type_val {
|
||||
dst & !([< get_mask_ $type_val >](range) << (range.start as $type_val))
|
||||
}
|
||||
|
||||
pub fn [< set_value_ $type_val >](dst: $type_val, value: $type_val, range: &BitRange) -> $type_val {
|
||||
[< clear_value_ $type_val >](dst, range) | ((value & [< get_mask_ $type_val >](range)) << range.start)
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
create_bit_functions!(u16);
|
|
@ -1,5 +1,7 @@
|
|||
use std::{boxed::Box, io::{Read, Write}, path::PathBuf};
|
||||
|
||||
pub mod bits;
|
||||
|
||||
pub type Output = Box<dyn Write>;
|
||||
pub type Input = Box<dyn Read>;
|
||||
|
||||
|
|
Loading…
Reference in New Issue