diff --git a/src/buint/mod.rs b/src/buint/mod.rs index 0614d95..5b8714a 100644 --- a/src/buint/mod.rs +++ b/src/buint/mod.rs @@ -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 diff --git a/src/doc/mod.rs b/src/doc/mod.rs index 333869d..ba1b356 100644 --- a/src/doc/mod.rs +++ b/src/doc/mod.rs @@ -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! {