Skip to content

Commit

Permalink
Merge pull request #44 from krakow10/set-bit
Browse files Browse the repository at this point in the history
add set_bit to buint
  • Loading branch information
isaacholt100 authored Sep 18, 2024
2 parents 9bb4d9f + 054250f commit 8e207de
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/buint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -462,6 +462,15 @@ macro_rules! mod_impl {
digit & (1 << (index & digit::$Digit::BITS_MINUS_1)) != 0
}

#[doc = doc::set_bit!(U 256)]
#[must_use]
#[inline]
pub fn set_bit(&mut self, index: ExpType, value: bool) {
let digit = &mut self.digits[index as usize >> digit::$Digit::BIT_SHIFT];
let shift = index & digit::$Digit::BITS_MINUS_1;
*digit = *digit & (1 << shift) | ($Digit::from(value) << shift)
}

/// Returns an integer whose value is `2^power`. This is faster than using a shift left on `Self::ONE`.
///
/// # Panics
Expand Down
16 changes: 16 additions & 0 deletions src/doc/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,22 @@ macro_rules! bit {

pub(crate) use bit;

macro_rules! set_bit {
($sign: ident $bits: literal) => {
doc::doc_comment! {
$sign $bits,
"Sets/unsets the bit in the given position (`1` if value is true). The least significant bit is at index `0`, the most significant bit is at index `Self::BITS - 1`",

"let mut n = " doc::type_str!($sign $bits) "::from(0b001010100101010101u32);\n"
"assert!(n.bit(2));\n"
"n.set_bit(2,false);\n"
"assert!(!n.bit(2));\n"
}
};
}

pub(crate) use set_bit;

macro_rules! is_zero {
($sign: ident $bits: literal) => {
doc::doc_comment! {
Expand Down

0 comments on commit 8e207de

Please sign in to comment.