Skip to main content

crypto_bigint/limb/
bit_xor.rs

1//! Limb bit xor operations.
2
3use super::Limb;
4use core::ops::{BitXor, BitXorAssign};
5
6impl Limb {
7    /// Calculates `a ^ b`.
8    #[inline(always)]
9    #[must_use]
10    pub const fn bitxor(self, rhs: Self) -> Self {
11        Limb(self.0 ^ rhs.0)
12    }
13}
14
15impl BitXor for Limb {
16    type Output = Limb;
17
18    fn bitxor(self, rhs: Self) -> Self::Output {
19        self.bitxor(rhs)
20    }
21}
22
23impl BitXor<&Self> for Limb {
24    type Output = Limb;
25
26    fn bitxor(self, rhs: &Self) -> Self::Output {
27        self.bitxor(*rhs)
28    }
29}
30
31impl BitXorAssign for Limb {
32    fn bitxor_assign(&mut self, rhs: Self) {
33        *self = self.bitxor(rhs);
34    }
35}
36
37impl BitXorAssign<&Limb> for Limb {
38    fn bitxor_assign(&mut self, rhs: &Self) {
39        *self = self.bitxor(*rhs);
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use crate::Limb;
46
47    #[test]
48    fn bitxor() {
49        assert_eq!(Limb::ZERO ^ Limb::ZERO, Limb::ZERO);
50        assert_eq!(Limb::ZERO ^ Limb::ONE, Limb::ONE);
51        assert_eq!(Limb::ONE ^ Limb::ZERO, Limb::ONE);
52        assert_eq!(Limb::ONE ^ Limb::ONE, Limb::ZERO);
53    }
54
55    #[test]
56    fn bitxor_assign() {
57        let mut n = Limb::ZERO;
58
59        n ^= Limb::ZERO;
60        assert_eq!(n, Limb::ZERO);
61
62        n ^= Limb::ONE;
63        assert_eq!(n, Limb::ONE);
64
65        n ^= Limb::ZERO;
66        assert_eq!(n, Limb::ONE);
67
68        n ^= Limb::ONE;
69        assert_eq!(n, Limb::ZERO);
70    }
71}