Skip to main content

crypto_bigint/uint/
bit_not.rs

1//! [`Uint`] bitwise NOT operations.
2
3use super::Uint;
4use crate::{Limb, Wrapping};
5use core::ops::Not;
6
7impl<const LIMBS: usize> Uint<LIMBS> {
8    /// Computes bitwise `!a`.
9    #[inline(always)]
10    #[must_use]
11    pub const fn not(&self) -> Self {
12        let mut limbs = [Limb::ZERO; LIMBS];
13        let mut i = 0;
14
15        while i < LIMBS {
16            limbs[i] = self.limbs[i].not();
17            i += 1;
18        }
19
20        Self { limbs }
21    }
22}
23
24impl<const LIMBS: usize> Not for Uint<LIMBS> {
25    type Output = Self;
26
27    fn not(self) -> Self {
28        Self::not(&self)
29    }
30}
31
32impl<const LIMBS: usize> Not for Wrapping<Uint<LIMBS>> {
33    type Output = Self;
34
35    fn not(self) -> <Self as Not>::Output {
36        Wrapping(self.0.not())
37    }
38}
39
40#[cfg(test)]
41mod tests {
42    use crate::U128;
43
44    #[test]
45    fn bitnot_ok() {
46        assert_eq!(U128::ZERO.not(), U128::MAX);
47        assert_eq!(U128::MAX.not(), U128::ZERO);
48    }
49}