about summary refs log tree commit diff
path: root/src/libcore
diff options
context:
space:
mode:
authorBrendan Zabarauskas <bjzaba@yahoo.com.au>2013-04-18 20:58:58 +1000
committerBrendan Zabarauskas <bjzaba@yahoo.com.au>2013-04-18 20:58:58 +1000
commitd2a81b95c3e2ccfdba0324caae531ce3528ed4e2 (patch)
tree362466ea31c22bcfa018bb126890899ce5b4a9fa /src/libcore
parent07e087bf310e7e7911bf05efa36a2cdb57855a4e (diff)
downloadrust-d2a81b95c3e2ccfdba0324caae531ce3528ed4e2.tar.gz
rust-d2a81b95c3e2ccfdba0324caae531ce3528ed4e2.zip
Implement bitwise operator traits for ints and uints
Diffstat (limited to 'src/libcore')
-rw-r--r--src/libcore/num/int-template.rs34
-rw-r--r--src/libcore/num/uint-template.rs35
2 files changed, 69 insertions, 0 deletions
diff --git a/src/libcore/num/int-template.rs b/src/libcore/num/int-template.rs
index b495f9e7088..f901e591027 100644
--- a/src/libcore/num/int-template.rs
+++ b/src/libcore/num/int-template.rs
@@ -199,6 +199,30 @@ impl ops::Modulo<T,T> for T {
 impl ops::Neg<T> for T {
     fn neg(&self) -> T { -*self }
 }
+#[cfg(notest)]
+impl ops::BitOr<T,T> for T {
+    fn bitor(&self, other: &T) -> T { *self | *other }
+}
+#[cfg(notest)]
+impl ops::BitAnd<T,T> for T {
+    fn bitand(&self, other: &T) -> T { *self & *other }
+}
+#[cfg(notest)]
+impl ops::BitXor<T,T> for T {
+    fn bitxor(&self, other: &T) -> T { *self ^ *other }
+}
+#[cfg(notest)]
+impl ops::Shl<T,T> for T {
+    fn shl(&self, other: &T) -> T { *self << *other }
+}
+#[cfg(notest)]
+impl ops::Shr<T,T> for T {
+    fn shr(&self, other: &T) -> T { *self >> *other }
+}
+#[cfg(notest)]
+impl ops::Not<T> for T {
+    fn not(&self) -> T { !*self }
+}
 
 // String conversion functions and impl str -> num
 
@@ -284,6 +308,16 @@ mod tests {
     use prelude::*;
 
     #[test]
+    fn test_bitwise_ops() {
+        assert!(0b1110 as T == (0b1100 as T).bitor(&(0b1010 as T)));
+        assert!(0b1000 as T == (0b1100 as T).bitand(&(0b1010 as T)));
+        assert!(0b0110 as T == (0b1100 as T).bitxor(&(0b1010 as T)));
+        assert!(0b1110 as T == (0b0111 as T).shl(&(1 as T)));
+        assert!(0b0111 as T == (0b1110 as T).shr(&(1 as T)));
+        assert!(-(0b11 as T) - (1 as T) == (0b11 as T).not());
+    }
+
+    #[test]
     fn test_from_str() {
         assert!(from_str(~"0") == Some(0 as T));
         assert!(from_str(~"3") == Some(3 as T));
diff --git a/src/libcore/num/uint-template.rs b/src/libcore/num/uint-template.rs
index af6557e9881..34c11804af4 100644
--- a/src/libcore/num/uint-template.rs
+++ b/src/libcore/num/uint-template.rs
@@ -164,6 +164,30 @@ impl ops::Modulo<T,T> for T {
 impl ops::Neg<T> for T {
     fn neg(&self) -> T { -*self }
 }
+#[cfg(notest)]
+impl ops::BitOr<T,T> for T {
+    fn bitor(&self, other: &T) -> T { *self | *other }
+}
+#[cfg(notest)]
+impl ops::BitAnd<T,T> for T {
+    fn bitand(&self, other: &T) -> T { *self & *other }
+}
+#[cfg(notest)]
+impl ops::BitXor<T,T> for T {
+    fn bitxor(&self, other: &T) -> T { *self ^ *other }
+}
+#[cfg(notest)]
+impl ops::Shl<T,T> for T {
+    fn shl(&self, other: &T) -> T { *self << *other }
+}
+#[cfg(notest)]
+impl ops::Shr<T,T> for T {
+    fn shr(&self, other: &T) -> T { *self >> *other }
+}
+#[cfg(notest)]
+impl ops::Not<T> for T {
+    fn not(&self) -> T { !*self }
+}
 
 // String conversion functions and impl str -> num
 
@@ -247,6 +271,17 @@ mod tests {
     use super::*;
     use super::inst::T;
     use prelude::*;
+
+    #[test]
+    fn test_bitwise_ops() {
+        assert!(0b1110 as T == (0b1100 as T).bitor(&(0b1010 as T)));
+        assert!(0b1000 as T == (0b1100 as T).bitand(&(0b1010 as T)));
+        assert!(0b0110 as T == (0b1100 as T).bitxor(&(0b1010 as T)));
+        assert!(0b1110 as T == (0b0111 as T).shl(&(1 as T)));
+        assert!(0b0111 as T == (0b1110 as T).shr(&(1 as T)));
+        assert!(max_value - (0b1011 as T) == (0b1011 as T).not());
+    }
+
     #[test]
     pub fn test_to_str() {
         assert!(to_str_radix(0 as T, 10u) == ~"0");