crypto_bigint/int/
bit_and.rs1use core::ops::{BitAnd, BitAndAssign};
4
5use crate::{CtOption, Int, Limb, Uint};
6
7impl<const LIMBS: usize> Int<LIMBS> {
8 #[inline(always)]
10 #[must_use]
11 pub const fn bitand(&self, rhs: &Self) -> Self {
12 Self(Uint::bitand(&self.0, &rhs.0))
13 }
14
15 #[must_use]
18 pub const fn bitand_limb(&self, rhs: Limb) -> Self {
19 Self(Uint::bitand_limb(&self.0, rhs))
20 }
21
22 #[must_use]
27 pub const fn wrapping_and(&self, rhs: &Self) -> Self {
28 self.bitand(rhs)
29 }
30
31 #[must_use]
33 pub const fn checked_and(&self, rhs: &Self) -> CtOption<Self> {
34 CtOption::some(self.bitand(rhs))
35 }
36}
37
38impl<const LIMBS: usize> BitAnd for Int<LIMBS> {
39 type Output = Self;
40
41 fn bitand(self, rhs: Self) -> Int<LIMBS> {
42 self.bitand(&rhs)
43 }
44}
45
46impl<const LIMBS: usize> BitAnd<&Int<LIMBS>> for Int<LIMBS> {
47 type Output = Int<LIMBS>;
48
49 #[allow(clippy::needless_borrow)]
50 fn bitand(self, rhs: &Int<LIMBS>) -> Int<LIMBS> {
51 (&self).bitand(rhs)
52 }
53}
54
55impl<const LIMBS: usize> BitAnd<Int<LIMBS>> for &Int<LIMBS> {
56 type Output = Int<LIMBS>;
57
58 fn bitand(self, rhs: Int<LIMBS>) -> Int<LIMBS> {
59 self.bitand(&rhs)
60 }
61}
62
63impl<const LIMBS: usize> BitAnd<&Int<LIMBS>> for &Int<LIMBS> {
64 type Output = Int<LIMBS>;
65
66 fn bitand(self, rhs: &Int<LIMBS>) -> Int<LIMBS> {
67 self.bitand(rhs)
68 }
69}
70
71impl<const LIMBS: usize> BitAndAssign for Int<LIMBS> {
72 #[allow(clippy::assign_op_pattern)]
73 fn bitand_assign(&mut self, other: Self) {
74 *self = *self & other;
75 }
76}
77
78impl<const LIMBS: usize> BitAndAssign<&Int<LIMBS>> for Int<LIMBS> {
79 #[allow(clippy::assign_op_pattern)]
80 fn bitand_assign(&mut self, other: &Self) {
81 *self = *self & other;
82 }
83}
84
85#[cfg(test)]
86mod tests {
87 use crate::I128;
88
89 #[test]
90 fn checked_and_ok() {
91 assert_eq!(I128::ZERO.checked_and(&I128::ONE).unwrap(), I128::ZERO);
92 assert_eq!(I128::ONE.checked_and(&I128::ONE).unwrap(), I128::ONE);
93 assert_eq!(I128::MAX.checked_and(&I128::ONE).unwrap(), I128::ONE);
94 }
95
96 #[test]
97 fn wrapping_and_ok() {
98 assert_eq!(I128::ZERO.wrapping_and(&I128::ONE), I128::ZERO);
99 assert_eq!(I128::ONE.wrapping_and(&I128::ONE), I128::ONE);
100 assert_eq!(I128::MAX.wrapping_and(&I128::ONE), I128::ONE);
101 }
102}