Skip to content

Instantly share code, notes, and snippets.

@jedjoud10
Created November 18, 2023 04:44
Show Gist options
  • Save jedjoud10/03458ff14d4ff587e0b61d2412af1d52 to your computer and use it in GitHub Desktop.
Save jedjoud10/03458ff14d4ff587e0b61d2412af1d52 to your computer and use it in GitHub Desktop.
Bitwise operation utilities in rust. Uses PrimInt from the num_traits crate.
/// Update a value in a specific bitmask, though return the unwritten value first
pub fn toggle_bit<T: PrimInt>(bitmask: &mut T, index: usize, value: bool) -> bool {
let copy = ((*bitmask >> index) & T::one()) == T::one();
if value {
*bitmask = *bitmask | (T::one() << index);
} else {
*bitmask = *bitmask & (!(T::one() << index));
}
copy
}
/// Enable all the bits between "start" and "end" in the binary representation of a T
/// Start is inclusive, end is exclusive
pub fn enable_in_range<T: PrimInt>(start: usize, end: usize) -> T {
assert!(end >= start);
let bits = size_of::<T>() * 8;
if end == bits {
!((T::one() << (start)) - T::one())
} else if start == bits {
T::zero()
} else {
((T::one() << (start)) - T::one()) ^ ((T::one() << end) - T::one())
}
}
/// Check if a bit at a specific index is set
pub fn is_bit_enabled<T: PrimInt>(bitset: T, index: usize) -> bool {
(bitset >> index & T::one()) == T::one()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment