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
//! Parity check for [`U512`] values.
use super::U512;
impl U512 {
/// Returns `true` if the least significant bit is set (the value is odd).
///
/// Checks bit 0 of the least significant limb (index 0).
///
/// # Examples
///
/// ```
/// use cnfy_uint::u512::U512;
///
/// assert!(!U512::ZERO.is_odd());
/// assert!(U512::ONE.is_odd());
/// assert!(U512::MAX.is_odd());
/// ```
#[inline]
pub fn is_odd(&self) -> bool {
self.0[0] & 1 == 1
}
}
#[cfg(test)]
mod ai_tests {
use super::*;
/// Zero is even.
#[test]
fn zero_is_even() {
assert!(!U512::ZERO.is_odd());
}
/// One is odd.
#[test]
fn one_is_odd() {
assert!(U512::ONE.is_odd());
}
/// Two is even.
#[test]
fn two_is_even() {
assert!(!U512::from_be_limbs([0, 0, 0, 0, 0, 0, 0, 2]).is_odd());
}
/// MAX (all bits set) is odd.
#[test]
fn max_is_odd() {
assert!(U512::MAX.is_odd());
}
/// A large even number with only the MSB limb set.
#[test]
fn large_even() {
let v = U512::from_be_limbs([0x8000_0000_0000_0000, 0, 0, 0, 0, 0, 0, 0]);
assert!(!v.is_odd());
}
/// A large odd number with MSB and LSB set.
#[test]
fn large_odd() {
let v = U512::from_be_limbs([0x8000_0000_0000_0000, 0, 0, 0, 0, 0, 0, 1]);
assert!(v.is_odd());
}
}