X-Git-Url: https://mograsim.net/gitweb/?a=blobdiff_plain;f=net.mograsim.logic.core%2Fsrc%2Fnet%2Fmograsim%2Flogic%2Fcore%2Ftypes%2FBitVector.java;h=134669494852256df8868924a63aa687e8b1fb00;hb=8bed58cd47f4e53a0a83e066d38864aa6875502f;hp=18b2d953054b34860ed21b61c68ccdc0cd4f4b5d;hpb=0457df6e156a2d6dddf8cc5af4caf6c041db7e80;p=Mograsim.git diff --git a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java index 18b2d953..13466949 100644 --- a/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java +++ b/net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java @@ -57,6 +57,19 @@ public final class BitVector implements StrictLogicType, Iterable, Iterable, Iterable + * An empty BitVectorMutator must not be converted to BitVector or used to manipulate single bits until at least one two + * operand logic operation is performed. */ public static BitVectorMutator empty() { return new BitVectorMutator(null); } + /** + * @see #empty() + */ + public boolean isEmpty() + { + return bits == null; + } + /** * Produces the resulting, immutable {@link BitVector}
* @@ -268,6 +307,8 @@ public final class BitVector implements StrictLogicType, Iterable, Iterable, Iterable, Iterable, Iterableoffset to the right.
- * Therefore offset + other.length() <= this.length() needs to be true. + * Therefore offset + other.length() <= this.wdith() needs to be true. * * @throws ArrayIndexOutOfBoundsException if offset + other.length() > this.length() * @@ -361,6 +410,58 @@ public final class BitVector implements StrictLogicType, Iterable= 0; i--) + { + if (Bit.ONE == bits[bits.length - i - 1]) + { + try + { + bytes[bytes.length - (i / 8) - 1] |= 1 << (i % 8); + } + catch (IndexOutOfBoundsException e) + { + e.printStackTrace(); + } + } + } + return new BigInteger(bytes); + } + + public long getUnsignedValueLong() + { + return getUnsignedValue().longValue(); + } + + /** + * Returns the value of the BitVector as BigInteger interpreted as a two's complement number. + * + * @throws NumberFormatException if the BitVector is not {@link #isBinary() binary}. + * + * @author Daniel Kirschten + */ + public BigInteger getSignedValue() + { + BigInteger unsignedValue = getUnsignedValue(); + if (bits[bits.length - 1] == Bit.ZERO) + return unsignedValue; + return unsignedValue.subtract(BitVector.of(Bit.ONE, bits.length).getUnsignedValue()).subtract(BigInteger.ONE);// TODO speed this up! + } + + public long getSignedValueLong() + { + return getSignedValue().longValue(); + } + /** * Parses a String containing solely {@link Bit} symbols (MSB first) * @@ -376,19 +477,18 @@ public final class BitVector implements StrictLogicType, Iterable singleBitOperation) { - 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); + Bit[] newBits = bits.clone(); + newBits[msbIndex] = singleBitOperation.apply(newBits[msbIndex]); + return new BitVector(newBits); } /** @@ -416,4 +516,15 @@ public final class BitVector implements StrictLogicType, Iterable