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
//! Bitwise NOT via the [`Not`] trait.
use super::U512;
use core::ops::Not;
/// Computes the bitwise complement of a 512-bit integer, flipping
/// every bit.
///
/// Applied independently to each of the eight `u64` limbs via the
/// `!` operator.
///
/// # Examples
///
/// ```
/// use cnfy_uint::u512::U512;
///
/// assert_eq!(!U512::ZERO, U512::MAX);
/// assert_eq!(!U512::MAX, U512::ZERO);
/// ```
impl Not for U512 {
type Output = U512;
#[inline]
fn not(self) -> U512 {
U512([
!self.0[0], !self.0[1], !self.0[2], !self.0[3],
!self.0[4], !self.0[5], !self.0[6], !self.0[7],
])
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// NOT zero is MAX.
#[test]
fn not_zero() {
assert_eq!(!U512::ZERO, U512::MAX);
}
/// NOT MAX is zero.
#[test]
fn not_max() {
assert_eq!(!U512::MAX, U512::ZERO);
}
/// Double NOT is identity.
#[test]
fn double_not() {
let a = U512::from_be_limbs([0x1234, 0x5678, 0x9ABC, 0xDEF0, 1, 2, 3, 4]);
assert_eq!(!!a, a);
}
/// NOT flips specific bits.
#[test]
fn flips_bits() {
let a = U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 0xFF]);
let expected = U512::from_be_limbs([
u64::MAX, u64::MAX, u64::MAX, u64::MAX,
u64::MAX, u64::MAX, u64::MAX, u64::MAX ^ 0xFF,
]);
assert_eq!(!a, expected);
}
}