pub trait GetBit {
// Required method
fn get_bit(&self, idx: usize) -> Option<bool>;
}
Expand description
performing _ & (1 << idx) with n the index and return the boolean corresponding
§Examples
§get bit for big int
Don’t pay attention to my bigint implementation.
pub struct BigInt {
hwb: i64,
lwb: i64,
}
impl GetBit for BigInt {
fn get_bit(&self, idx: usize) -> Option<bool> {
if idx >= 128 {
None
} else if idx >= 64 {
if self.hwb & (1 << (idx - 64)) != 0 {
Some(true)
}
else {
Some(false)
}
} else {
if self.lwb & (1 << idx) != 0 {
Some(true)
}
else {
Some(false)
}
}
}
}