add BitVector copy constructor and BitVector.setAndSignExtend()
authoradam <adam@megacz.com>
Thu, 26 Jun 2008 10:32:14 +0000 (11:32 +0100)
committeradam <adam@megacz.com>
Thu, 26 Jun 2008 10:32:14 +0000 (11:32 +0100)
src/edu/berkeley/fleet/api/BitVector.java

index c883b3b..9282200 100644 (file)
@@ -15,6 +15,13 @@ public class BitVector {
         this.bits = new boolean[length];
     }
 
+    /** copy constructor */
+    public BitVector(BitVector bv) {
+        this(bv.length());
+        for(int i=0; i<bv.length(); i++)
+            set(i, bv.get(i));
+    }
+
     /** copy the low-order bits of the argument into this BitVector; returns <tt>this</tt> */
     public BitVector set(long value) {
         if (immutable) throw new RuntimeException("attempt to modify an immutable BitVector");
@@ -23,6 +30,17 @@ public class BitVector {
         return this;
     }
 
+    /** copy the low-order bits of the argument into this BitVector and sign extend; returns <tt>this</tt> */
+    public BitVector setAndSignExtend(long value) {
+        if (immutable) throw new RuntimeException("attempt to modify an immutable BitVector");
+        for(int i=0; i<Math.min(length(), 64); i++)
+            set(i, ((value >>> i) & 1L) != 0);
+        if (value < 0)
+            for(int i=64; i<length(); i++)
+                set(i, true);
+        return this;
+    }
+
     public int length() {
         return bits.length;
     }