From 0c33e0d7df4bf905be677b868fa63536efd45d56 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Thu, 29 Aug 2024 19:45:38 -0700 Subject: [PATCH 1/2] add set_bit to buint --- src/buint/mod.rs | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/buint/mod.rs b/src/buint/mod.rs index 3d00417..c45289f 100644 --- a/src/buint/mod.rs +++ b/src/buint/mod.rs @@ -455,6 +455,15 @@ macro_rules! mod_impl { digit & (1 << (index & digit::$Digit::BITS_MINUS_1)) != 0 } + #[doc = doc::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 From 054250f8d0f74b9339305a83fdb910be760d46b3 Mon Sep 17 00:00:00 2001 From: Quaternions Date: Wed, 4 Sep 2024 18:16:57 -0700 Subject: [PATCH 2/2] write doc for set_bit --- src/buint/mod.rs | 2 +- src/doc/mod.rs | 16 ++++++++++++++++ 2 files changed, 17 insertions(+), 1 deletion(-) diff --git a/src/buint/mod.rs b/src/buint/mod.rs index c45289f..7a6c7f1 100644 --- a/src/buint/mod.rs +++ b/src/buint/mod.rs @@ -455,7 +455,7 @@ macro_rules! mod_impl { digit & (1 << (index & digit::$Digit::BITS_MINUS_1)) != 0 } - #[doc = doc::bit!(U 256)] + #[doc = doc::set_bit!(U 256)] #[must_use] #[inline] pub fn set_bit(&mut self, index: ExpType, value: bool) { 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! {