rhdl_bits/
and.rs

1use std::ops::{BitAnd, BitAndAssign};
2
3use crate::{bits::Bits, signed_bits::SignedBits};
4
5impl<const N: usize> BitAnd<Bits<N>> for u128 {
6    type Output = Bits<N>;
7    fn bitand(self, rhs: Bits<N>) -> Self::Output {
8        Bits::<N>::from(self) & rhs
9    }
10}
11
12impl<const N: usize> BitAnd<u128> for Bits<N> {
13    type Output = Self;
14    fn bitand(self, rhs: u128) -> Self::Output {
15        self & Bits::<N>::from(rhs)
16    }
17}
18
19impl<const N: usize> BitAndAssign<u128> for Bits<N> {
20    fn bitand_assign(&mut self, rhs: u128) {
21        *self = *self & rhs;
22    }
23}
24
25impl<const N: usize> BitAnd<SignedBits<N>> for i128 {
26    type Output = SignedBits<N>;
27    fn bitand(self, rhs: SignedBits<N>) -> Self::Output {
28        SignedBits::<N>::from(self) & rhs
29    }
30}
31
32impl<const N: usize> BitAnd<i128> for SignedBits<N> {
33    type Output = Self;
34    fn bitand(self, rhs: i128) -> Self::Output {
35        self & SignedBits::<N>::from(rhs)
36    }
37}
38
39impl<const N: usize> BitAndAssign<i128> for SignedBits<N> {
40    fn bitand_assign(&mut self, rhs: i128) {
41        *self = *self & rhs;
42    }
43}
44
45#[cfg(test)]
46mod test {
47    use super::*;
48
49    #[test]
50    fn test_and_bits() {
51        let bits: Bits<8> = 0b1101_1010.into();
52        let result = bits & bits;
53        assert_eq!(result.0, 0b1101_1010_u128);
54        let bits: Bits<8> = 0b1101_1010.into();
55        let result = bits & 0b1111_0000;
56        assert_eq!(result.0, 0b1101_0000_u128);
57        let bits: Bits<8> = 0b1101_1010.into();
58        let result = 0b1111_0000 & bits;
59        assert_eq!(result.0, 0b1101_0000_u128);
60        let mut bits: Bits<128> = 0.into();
61        bits.set_bit(127, true);
62        let result = bits & bits;
63        assert_eq!(result.0, 1_u128 << 127);
64        let bits: Bits<54> = 0b1101_1010.into();
65        let result = bits & 1;
66        assert_eq!(result.0, 0_u128);
67        let result = 1 & bits;
68        assert_eq!(result.0, 0_u128);
69    }
70
71    #[test]
72    fn test_andassign_bits() {
73        let mut bits: Bits<8> = 0b1101_1010.into();
74        bits &= bits;
75        assert_eq!(bits.0, 0b1101_1010_u128);
76        let mut bits: Bits<8> = 0b1101_1010.into();
77        bits &= 0b1111_0000;
78        assert_eq!(bits.0, 0b1101_0000_u128);
79        let mut bits: Bits<8> = 0b1101_1010.into();
80        bits &= 0b1111_0000;
81        assert_eq!(bits.0, 0b1101_0000_u128);
82        let mut bits: Bits<128> = 0.into();
83        bits.set_bit(127, true);
84        bits &= bits;
85        assert_eq!(bits.0, 1_u128 << 127);
86        let mut bits: Bits<54> = 0b1101_1010.into();
87        bits &= 1;
88        assert_eq!(bits.0, 0_u128);
89        let mut bits: Bits<54> = 0b1101_1010.into();
90        bits &= 1;
91        assert_eq!(bits.0, 0_u128);
92        let a: Bits<12> = 0b1010_1010_1010.into();
93        let b: Bits<12> = 0b1111_0101_0111.into();
94        let mut c = a;
95        c &= b;
96        assert_eq!(c.0, 0b1010_0000_0010);
97    }
98
99    #[test]
100    fn test_anding_is_weird_for_signed() {
101        let x = -3;
102        let y = 4;
103        let z = x & y;
104        assert!(z > 0);
105    }
106
107    #[test]
108    fn test_and_assign_signed() {
109        let mut x = SignedBits::<8>::from(-3);
110        let y = SignedBits::<8>::from(4);
111        x &= y;
112        assert_eq!(x.0, 4);
113    }
114
115    #[test]
116    fn test_and_signed() {
117        let x = SignedBits::<8>::from(-3);
118        let y = SignedBits::<8>::from(4);
119        let z = x & y;
120        assert_eq!(z.0, 4);
121    }
122
123    #[test]
124    fn test_and_signed_i8() {
125        for i in i8::MIN..i8::MAX {
126            for j in i8::MIN..i8::MAX {
127                let x: SignedBits<8> = (i as i128).into();
128                let y: SignedBits<8> = (j as i128).into();
129                let z = x & y;
130                assert_eq!(z.0, (i & j).into());
131            }
132        }
133    }
134
135    #[test]
136    fn test_and_assign_signed_i8() {
137        for i in i8::MIN..i8::MAX {
138            for j in i8::MIN..i8::MAX {
139                let mut x: SignedBits<8> = (i as i128).into();
140                let y: SignedBits<8> = (j as i128).into();
141                x &= y;
142                assert_eq!(x.0, (i & j).into());
143            }
144        }
145    }
146}