Struct xor_distance_exercise::bits::Bits
source · pub struct Bits { /* private fields */ }
Expand description
Bits representation.
Examples
extern crate xor_distance_exercise;
use xor_distance_exercise::bits::Bits;
// Find out bit size of specific integer type.
let size = Bits::bit_size::<i64>();
// Bit representation of `u64` integer.
let mut bit_rep = Bits::new::<u64>();
// Operations on the bit representation.
let bit = bit_rep.get_bit(4);
bit_rep.set_bit(4, true);
bit_rep.set_bit_within_constrains(5, true);
bit_rep.is_bit_decided(4);
let number = bit_rep.form_zero_padded_number::<u64>().unwrap();
Implementations
sourceimpl Bits
impl Bits
sourcepub fn new<T: PrimInt>() -> Self
pub fn new<T: PrimInt>() -> Self
Create a new representation of Bits.
Examples
extern crate xor_distance_exercise;
use xor_distance_exercise::bits::Bits;
let bit_rep = Bits::new::<u64>;
sourcepub fn bit_size<T: PrimInt>() -> usize
pub fn bit_size<T: PrimInt>() -> usize
Return bit size of the type being represented in bits.
Examples
extern crate xor_distance_exercise;
use xor_distance_exercise::bits::Bits;
assert_eq!(8, Bits::bit_size::<u8>());
assert_eq!(32, Bits::bit_size::<u32>());
assert_eq!(64, Bits::bit_size::<u64>());
assert_eq!(64, Bits::bit_size::<i64>());
sourcepub fn set_bit_within_constrains(
&mut self,
index: usize,
val: bool
) -> Result<(), &'static str>
pub fn set_bit_within_constrains(
&mut self,
index: usize,
val: bool
) -> Result<(), &'static str>
Set new bit value complying with constrains, already decided bit value can not be changed.
Returns Ok(())
in case constrains were not violated, Err(&str)
otherwise.
Examples
extern crate xor_distance_exercise;
use xor_distance_exercise::bits::Bits;
let mut bit_rep = Bits::new::<u64>();
bit_rep.set_bit_within_constrains(4, true);
Panics
Panics if index
is out of range.
sourcepub fn is_bit_decided(&self, index: usize) -> bool
pub fn is_bit_decided(&self, index: usize) -> bool
sourcepub fn form_zero_padded_number<T: PrimInt>(&self) -> Result<T, &str>
pub fn form_zero_padded_number<T: PrimInt>(&self) -> Result<T, &str>
Form and return a number based on bits representation, pad/fill undecided bits by zeros.
Examples
extern crate xor_distance_exercise;
use xor_distance_exercise::bits::Bits;
let bit_rep = Bits::new::<u64>();
let number = bit_rep.form_zero_padded_number::<u64>().unwrap();
Auto Trait Implementations
impl RefUnwindSafe for Bits
impl Send for Bits
impl Sync for Bits
impl Unpin for Bits
impl UnwindSafe for Bits
Blanket Implementations
sourceimpl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more