Added handy new BitVector manipulation method
[Mograsim.git] / net.mograsim.logic.core / src / net / mograsim / logic / core / types / BitVector.java
index e17ae6e..4d5f911 100644 (file)
@@ -66,9 +66,7 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
        {
                Bit[] values = new Bit[bits];
                for (int i = 0; i < bits; i++)
-               {
                        values[bits - i - 1] = Bit.of(value.testBit(i));
-               }
                return new BitVector(values);
        }
 
@@ -385,7 +383,7 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
 
        /**
         * Does test for equality of values/content, shifting the other BitVector by <code>offset</code> to the right.<br>
-        * Therefore <code>offset + other.length() <= this.length()</code> needs to be true.
+        * Therefore <code>offset + other.length() <= this.wdith()</code> needs to be true.
         * 
         * @throws ArrayIndexOutOfBoundsException if <code>offset + other.length() > this.length()</code>
         * 
@@ -421,12 +419,19 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
        {
                if (!isBinary())
                        throw new NumberFormatException(this + " is not binary");
-               byte[] bytes = new byte[(bits.length / 8) + 1];
-               for (int i = 0; i < bits.length; i++)
+               byte[] bytes = new byte[(bits.length / 8 + (bits.length % 8 == 0 ? 0 : 1)) + 1];
+               for (int i = bits.length - 1; i >= 0; i--)
                {
-                       if (Bit.ONE == bits[i])
+                       if (Bit.ONE == bits[bits.length - i - 1])
                        {
-                               bytes[i / 8] |= 1 << (i % 8);
+                               try
+                               {
+                                       bytes[bytes.length - (i / 8) - 1] |= 1 << (i % 8);
+                               }
+                               catch (IndexOutOfBoundsException e)
+                               {
+                                       e.printStackTrace();
+                               }
                        }
                }
                return new BigInteger(bytes);
@@ -447,6 +452,20 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                return new BitVector(values);
        }
 
+       /**
+        * Changes a single Bit using the given operation. This can be used to set, clear or flip bits.
+        * 
+        * @param msbIndex           index of the MSB to be changed
+        * @param singleBitOperation the operation to perform on that Bit
+        * @return the resulting, new BitVektor
+        */
+       public BitVector withBitChanged(int msbIndex, UnaryOperator<Bit> singleBitOperation)
+       {
+               Bit[] newBits = bits.clone();
+               newBits[msbIndex] = singleBitOperation.apply(newBits[msbIndex]);
+               return new BitVector(newBits);
+       }
+
        /**
         * Iterate over the {@link Bit}s of the BitVector <b>from MSB to LSB</b> (left to right).
         */