pub trait BitHelper {
    // Required methods
    fn read_bit(&self, pos: u32) -> bool;
    fn write_bit(&mut self, pos: u32, val: bool) -> &mut Self;
    fn read_bits_in_range(&self, bit_range: &BitRange) -> Self;
    fn write_bits_in_range(
        &mut self,
        bit_range: &BitRange,
        val: Self
    ) -> &mut Self;
}
Expand description

Trait containing helper methods for bit operations.

Required Methods§

source

fn read_bit(&self, pos: u32) -> bool

Reads the value of the bit at position pos

source

fn write_bit(&mut self, pos: u32, val: bool) -> &mut Self

Changes the value of the bit at position pos to val

source

fn read_bits_in_range(&self, bit_range: &BitRange) -> Self

Reads the value stored within the specified range of bits

Example
#[macro_use]
use dbs_arch::cpuid::bit_helper::*;

let val: u32 = 0b000010001000;
let range = BitRange {
    msb_index: 7,
    lsb_index: 3,
};
println!("binary value: {:b}", val.read_bits_in_range(&range));

The code above will print:

binary value: 10001
source

fn write_bits_in_range(&mut self, bit_range: &BitRange, val: Self) -> &mut Self

Stores a value within the specified range of bits

Example
#[macro_use]
use dbs_arch::cpuid::bit_helper::*;

let mut val: u32 = 0;
let range = BitRange {
    msb_index: 7,
    lsb_index: 3,
};
val.write_bits_in_range(&range, 0b10001 as u32);
println!("binary value: {:b}", val);

The code above will print:

binary value: 10001000

Implementations on Foreign Types§

source§

impl BitHelper for u32

source§

fn read_bit(&self, pos: u32) -> bool

source§

fn write_bit(&mut self, pos: u32, val: bool) -> &mut Self

source§

fn read_bits_in_range(&self, range: &BitRange) -> Self

source§

fn write_bits_in_range(&mut self, range: &BitRange, val: Self) -> &mut Self

Implementors§