Added handy new BitVector manipulation method
authorChristian Femers <femers@in.tum.de>
Mon, 2 Sep 2019 02:09:23 +0000 (04:09 +0200)
committerChristian Femers <femers@in.tum.de>
Mon, 2 Sep 2019 02:09:23 +0000 (04:09 +0200)
net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java

index 4e0fe0b..4d5f911 100644 (file)
@@ -452,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).
         */