@@ -153,6 +153,43 @@ impl Orderable for BigUint {
153
153
}
154
154
}
155
155
156
+ impl BitAnd < BigUint , BigUint > for BigUint {
157
+ fn bitand ( & self , other : & BigUint ) -> BigUint {
158
+ let new_len = num:: min ( self . data . len ( ) , other. data . len ( ) ) ;
159
+ let anded = do vec:: from_fn ( new_len) |i| {
160
+ // i will never be less than the size of either data vector
161
+ let ai = self . data [ i] ;
162
+ let bi = other. data [ i] ;
163
+ ai & bi
164
+ } ;
165
+ return BigUint :: new ( anded) ;
166
+ }
167
+ }
168
+
169
+ impl BitOr < BigUint , BigUint > for BigUint {
170
+ fn bitor ( & self , other : & BigUint ) -> BigUint {
171
+ let new_len = num:: max ( self . data . len ( ) , other. data . len ( ) ) ;
172
+ let ored = do vec:: from_fn ( new_len) |i| {
173
+ let ai = if i < self . data . len ( ) { self . data [ i] } else { 0 } ;
174
+ let bi = if i < other. data . len ( ) { other. data [ i] } else { 0 } ;
175
+ ai | bi
176
+ } ;
177
+ return BigUint :: new ( ored) ;
178
+ }
179
+ }
180
+
181
+ impl BitXor < BigUint , BigUint > for BigUint {
182
+ fn bitxor ( & self , other : & BigUint ) -> BigUint {
183
+ let new_len = num:: max ( self . data . len ( ) , other. data . len ( ) ) ;
184
+ let xored = do vec:: from_fn ( new_len) |i| {
185
+ let ai = if i < self . data . len ( ) { self . data [ i] } else { 0 } ;
186
+ let bi = if i < other. data . len ( ) { other. data [ i] } else { 0 } ;
187
+ ai ^ bi
188
+ } ;
189
+ return BigUint :: new ( xored) ;
190
+ }
191
+ }
192
+
156
193
impl Shl < uint , BigUint > for BigUint {
157
194
#[ inline]
158
195
fn shl ( & self , rhs : & uint ) -> BigUint {
@@ -1166,6 +1203,48 @@ mod biguint_tests {
1166
1203
}
1167
1204
}
1168
1205
1206
+ #[test]
1207
+ fn test_bitand() {
1208
+ fn check(left: ~[BigDigit],
1209
+ right: ~[BigDigit],
1210
+ expected: ~[BigDigit]) {
1211
+ assert_eq!(BigUint::new(left) & BigUint::new(right),
1212
+ BigUint::new(expected));
1213
+ }
1214
+ check(~[], ~[], ~[]);
1215
+ check(~[268, 482, 17],
1216
+ ~[964, 54],
1217
+ ~[260, 34]);
1218
+ }
1219
+
1220
+ #[test]
1221
+ fn test_bitor() {
1222
+ fn check(left: ~[BigDigit],
1223
+ right: ~[BigDigit],
1224
+ expected: ~[BigDigit]) {
1225
+ assert_eq!(BigUint::new(left) | BigUint::new(right),
1226
+ BigUint::new(expected));
1227
+ }
1228
+ check(~[], ~[], ~[]);
1229
+ check(~[268, 482, 17],
1230
+ ~[964, 54],
1231
+ ~[972, 502, 17]);
1232
+ }
1233
+
1234
+ #[test]
1235
+ fn test_bitxor() {
1236
+ fn check(left: ~[BigDigit],
1237
+ right: ~[BigDigit],
1238
+ expected: ~[BigDigit]) {
1239
+ assert_eq!(BigUint::new(left) ^ BigUint::new(right),
1240
+ BigUint::new(expected));
1241
+ }
1242
+ check(~[], ~[], ~[]);
1243
+ check(~[268, 482, 17],
1244
+ ~[964, 54],
1245
+ ~[712, 468, 17]);
1246
+ }
1247
+
1169
1248
#[test]
1170
1249
fn test_shl() {
1171
1250
fn check(s: &str, shift: uint, ans: &str) {
0 commit comments