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=6a36403475c1ef7a2f5d5296b98d9a5268f046b8;hpb=fb17fab5f3a145173dcd0b4b7d6e6553dbcd2e54;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 6a364034..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,49 +57,16 @@ public final class BitVector implements StrictLogicType, 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() * @@ -439,20 +411,55 @@ 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(); } /** @@ -470,6 +477,20 @@ public final class BitVector implements StrictLogicType, Iterable singleBitOperation) + { + Bit[] newBits = bits.clone(); + newBits[msbIndex] = singleBitOperation.apply(newBits[msbIndex]); + return new BitVector(newBits); + } + /** * Iterate over the {@link Bit}s of the BitVector from MSB to LSB (left to right). */ @@ -495,4 +516,15 @@ public final class BitVector implements StrictLogicType, Iterable