1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
//! Bitwise AND via the [`BitAnd`] trait.
use super::U512;
use core::ops::BitAnd;
/// Computes the bitwise AND of two 512-bit integers, producing a
/// result where each bit is set only if both corresponding input bits
/// are set.
///
/// Applied independently to each of the eight `u64` limbs.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u512::U512;
///
/// let a = U512::from_be_limbs([0xFF; 8]);
/// let b = U512::from_be_limbs([0x0F; 8]);
/// assert_eq!(a & b, U512::from_be_limbs([0x0F; 8]));
/// ```
impl BitAnd for U512 {
type Output = U512;
#[inline]
fn bitand(self, rhs: U512) -> U512 {
U512([
self.0[0] & rhs.0[0],
self.0[1] & rhs.0[1],
self.0[2] & rhs.0[2],
self.0[3] & rhs.0[3],
self.0[4] & rhs.0[4],
self.0[5] & rhs.0[5],
self.0[6] & rhs.0[6],
self.0[7] & rhs.0[7],
])
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// AND with self is identity.
#[test]
fn self_identity() {
let a = U512::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 1, 2, 3, 4]);
assert_eq!(a & a, a);
}
/// AND with zero is zero.
#[test]
fn and_zero() {
let a = U512::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 1, 2, 3, 4]);
assert_eq!(a & U512::ZERO, U512::ZERO);
}
/// AND with MAX is identity.
#[test]
fn and_max() {
let a = U512::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 1, 2, 3, 4]);
assert_eq!(a & U512::MAX, a);
}
/// Masking extracts specific bits.
#[test]
fn mask() {
let a = U512::from_be_limbs([0xFF00, 0, 0, 0, 0, 0, 0, 0]);
let mask = U512::from_be_limbs([0x0F00, 0, 0, 0, 0, 0, 0, 0]);
assert_eq!(a & mask, U512::from_be_limbs([0x0F00, 0, 0, 0, 0, 0, 0, 0]));
}
/// AND is commutative.
#[test]
fn commutative() {
let a = U512::from_be_limbs([1, 2, 3, 4, 5, 6, 7, 8]);
let b = U512::from_be_limbs([8, 7, 6, 5, 4, 3, 2, 1]);
assert_eq!(a & b, b & a);
}
}