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
#![feature(trait_alias)]
#![feature(test)]
#![no_std]
mod bit_operation;
mod bit_string;
/// Set `num_of_bits` bits from the `start_bit`th bit of address `address`.
///
/// `num_of_bits` may be more than the number of bits a byte has.
///
/// # Examples
///
/// Set 3 bits from the 2nd bit of specified address.
///
/// ```
/// let byte: Box<u32> = Box::new(0);
/// let ptr = Box::into_raw(byte);
///
/// set_bits::set(ptr as usize, 2, 3);
/// unsafe {
/// assert_eq!(*ptr, 0b11100);
/// }
///
/// // For automatic cleanup.
/// // See the example of into_raw function.
/// // https://doc.rust-lang.org/std/boxed/struct.Box.html
/// let byte = unsafe { Box::from_raw(ptr) };
/// ```
pub fn set(address: usize, start_bit: usize, num_of_bits: usize) -> () {
bit_operation::bit_operation(
bit_string::BitString::new(address, start_bit, num_of_bits),
bit_operation::Operation::Set,
);
}
/// Clear `num_of_bits` bits from the `start_bit`th bit of address `address`.
///
/// `num_of_bits` may be more than the number of bits a byte has.
///
/// # Examples
///
/// Clear 3 bits from the 2nd bit of specified address.
///
/// ```
/// let byte: Box<u32> = Box::new(0);
/// let ptr = Box::into_raw(byte);
///
/// set_bits::set(ptr as usize, 0, 8);
/// set_bits::clear(ptr as usize, 2, 3);
/// unsafe {
/// assert_eq!(*ptr, 0b11100011);
/// }
///
/// let byte = unsafe { Box::from_raw(ptr) };
/// ```
pub fn clear(address: usize, start_bit: usize, num_of_bits: usize) -> () {
bit_operation::bit_operation(
bit_string::BitString::new(address, start_bit, num_of_bits),
bit_operation::Operation::Clear,
);
}