From 437dcd34fb431ae5fddbcd61e56e504c655adb25 Mon Sep 17 00:00:00 2001 From: "Timothy J. Warren" Date: Thu, 5 Mar 2020 09:20:02 -0500 Subject: [PATCH] Implement NOT operator --- src/bigint.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/src/bigint.rs b/src/bigint.rs index 0a5f8cd..cbf9839 100644 --- a/src/bigint.rs +++ b/src/bigint.rs @@ -210,6 +210,23 @@ impl Neg for BigInt { } } +impl Not for BigInt { + type Output = Self; + + fn not(self) -> Self::Output { + let mut flipped: Vec = Vec::with_capacity(self.inner.len()); + + for (i, val) in self.inner.iter().enumerate() { + flipped[i] = val.reverse_bits(); + } + + BigInt { + sign: self.sign, + inner: flipped, + } + } +} + #[cfg(test)] mod tests { use super::*; @@ -237,6 +254,12 @@ mod tests { "least significant place should be MAX - 1" ); assert_eq!(sum.inner[1], 1usize, "most significant place should be 1"); + + /* let a = BigInt::from(core::usize::MAX); + let b = BigInt::from(1usize); + let sum = a + b; + assert_eq!(sum.inner[0], 0usize); + assert_eq!(sum.inner[1], 1usize); */ } #[test]