Skip to content

Commit d2a81b9

Browse files
committed
Implement bitwise operator traits for ints and uints
1 parent 07e087b commit d2a81b9

File tree

2 files changed

+69
-0
lines changed

2 files changed

+69
-0
lines changed

src/libcore/num/int-template.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,30 @@ impl ops::Modulo<T,T> for T {
199199
impl ops::Neg<T> for T {
200200
fn neg(&self) -> T { -*self }
201201
}
202+
#[cfg(notest)]
203+
impl ops::BitOr<T,T> for T {
204+
fn bitor(&self, other: &T) -> T { *self | *other }
205+
}
206+
#[cfg(notest)]
207+
impl ops::BitAnd<T,T> for T {
208+
fn bitand(&self, other: &T) -> T { *self & *other }
209+
}
210+
#[cfg(notest)]
211+
impl ops::BitXor<T,T> for T {
212+
fn bitxor(&self, other: &T) -> T { *self ^ *other }
213+
}
214+
#[cfg(notest)]
215+
impl ops::Shl<T,T> for T {
216+
fn shl(&self, other: &T) -> T { *self << *other }
217+
}
218+
#[cfg(notest)]
219+
impl ops::Shr<T,T> for T {
220+
fn shr(&self, other: &T) -> T { *self >> *other }
221+
}
222+
#[cfg(notest)]
223+
impl ops::Not<T> for T {
224+
fn not(&self) -> T { !*self }
225+
}
202226
203227
// String conversion functions and impl str -> num
204228
@@ -283,6 +307,16 @@ mod tests {
283307
use super::inst::T;
284308
use prelude::*;
285309
310+
#[test]
311+
fn test_bitwise_ops() {
312+
assert!(0b1110 as T == (0b1100 as T).bitor(&(0b1010 as T)));
313+
assert!(0b1000 as T == (0b1100 as T).bitand(&(0b1010 as T)));
314+
assert!(0b0110 as T == (0b1100 as T).bitxor(&(0b1010 as T)));
315+
assert!(0b1110 as T == (0b0111 as T).shl(&(1 as T)));
316+
assert!(0b0111 as T == (0b1110 as T).shr(&(1 as T)));
317+
assert!(-(0b11 as T) - (1 as T) == (0b11 as T).not());
318+
}
319+
286320
#[test]
287321
fn test_from_str() {
288322
assert!(from_str(~"0") == Some(0 as T));

src/libcore/num/uint-template.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,30 @@ impl ops::Modulo<T,T> for T {
164164
impl ops::Neg<T> for T {
165165
fn neg(&self) -> T { -*self }
166166
}
167+
#[cfg(notest)]
168+
impl ops::BitOr<T,T> for T {
169+
fn bitor(&self, other: &T) -> T { *self | *other }
170+
}
171+
#[cfg(notest)]
172+
impl ops::BitAnd<T,T> for T {
173+
fn bitand(&self, other: &T) -> T { *self & *other }
174+
}
175+
#[cfg(notest)]
176+
impl ops::BitXor<T,T> for T {
177+
fn bitxor(&self, other: &T) -> T { *self ^ *other }
178+
}
179+
#[cfg(notest)]
180+
impl ops::Shl<T,T> for T {
181+
fn shl(&self, other: &T) -> T { *self << *other }
182+
}
183+
#[cfg(notest)]
184+
impl ops::Shr<T,T> for T {
185+
fn shr(&self, other: &T) -> T { *self >> *other }
186+
}
187+
#[cfg(notest)]
188+
impl ops::Not<T> for T {
189+
fn not(&self) -> T { !*self }
190+
}
167191
168192
// String conversion functions and impl str -> num
169193
@@ -247,6 +271,17 @@ mod tests {
247271
use super::*;
248272
use super::inst::T;
249273
use prelude::*;
274+
275+
#[test]
276+
fn test_bitwise_ops() {
277+
assert!(0b1110 as T == (0b1100 as T).bitor(&(0b1010 as T)));
278+
assert!(0b1000 as T == (0b1100 as T).bitand(&(0b1010 as T)));
279+
assert!(0b0110 as T == (0b1100 as T).bitxor(&(0b1010 as T)));
280+
assert!(0b1110 as T == (0b0111 as T).shl(&(1 as T)));
281+
assert!(0b0111 as T == (0b1110 as T).shr(&(1 as T)));
282+
assert!(max_value - (0b1011 as T) == (0b1011 as T).not());
283+
}
284+
250285
#[test]
251286
pub fn test_to_str() {
252287
assert!(to_str_radix(0 as T, 10u) == ~"0");

0 commit comments

Comments
 (0)