rhdl_bits/
not.rs

1use std::ops::Not;
2
3use crate::{bits::Bits, signed_bits::SignedBits};
4
5impl<const N: usize> Not for Bits<N> {
6    type Output = Self;
7    fn not(self) -> Self::Output {
8        Self(!self.0 & Self::mask().0)
9    }
10}
11
12impl<const N: usize> Not for SignedBits<N> {
13    type Output = Self;
14    fn not(self) -> Self::Output {
15        self.as_unsigned().not().as_signed()
16    }
17}
18
19#[cfg(test)]
20mod test {
21    use super::*;
22
23    #[test]
24    fn test_not_bits() {
25        let bits: Bits<8> = 0b1101_1010.into();
26        let result = !bits;
27        assert_eq!(result.0, 0b0010_0101_u128);
28        let mut bits: Bits<128> = 0.into();
29        bits.set_bit(127, true);
30        let result = !bits;
31        assert_eq!(result.0, !0_u128 - (1 << 127));
32        let bits: Bits<14> = 0b1101_1010.into();
33        let result = !bits;
34        assert_eq!(result.0, 0b0011_1111_0010_0101_u128);
35    }
36
37    #[test]
38    fn test_not_on_i8() {
39        let x = -4_i8;
40        let result = !x;
41        assert_eq!(result, 3_i8);
42    }
43
44    #[test]
45    fn test_not_on_signed_bits() {
46        let x = SignedBits::<8>::from(-4);
47        let result = !x;
48        assert_eq!(result.0, 3_i128);
49    }
50
51    #[test]
52    fn test_not_on_signed_does_not_overflow() {
53        let x = SignedBits::<128>::from(-1);
54        let result = !x;
55        assert_eq!(result.0, 0_i128);
56    }
57}