Added methods for conversion between BitVector and BigInteger
authorFabian Stemmler <stemmler@in.tum.de>
Sun, 25 Aug 2019 14:53:21 +0000 (16:53 +0200)
committerFabian Stemmler <stemmler@in.tum.de>
Sun, 25 Aug 2019 14:53:21 +0000 (16:53 +0200)
net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java

index 3e5c177..e8a4098 100644 (file)
@@ -57,6 +57,39 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                return new BitVector(bit.makeArray(length));
        }
 
+       public BigInteger getUnsignedValue()
+       {
+               if (!isBinary())
+                       throw new NumberFormatException("BitVector is non binary: " + toString());
+               Bit[] bits = getBits();
+               int length = length();
+               byte[] bytes = new byte[(length / 8) + 1];
+               for (int i = 0; i < length; i++)
+               {
+                       if (Bit.ONE.equals(bits[i]))
+                       {
+                               bytes[(i / 8) + 1] |= 1 << (i % 8);
+                       }
+               }
+               return new BigInteger(bytes);
+       }
+
+       public static BitVector from(BigInteger b, int length)
+       {
+               int bitLength = b.bitLength();
+               int actualLength = Integer.min(bitLength, length);
+               Bit[] bits = new Bit[length];
+               for (int i = 0; i < actualLength; i++)
+                       bits[i] = b.testBit(i) ? Bit.ONE : Bit.ZERO;
+               if (b.signum() < 0)
+                       for (int i = actualLength; i < length; i++)
+                               bits[i] = Bit.ONE;
+               else
+                       for (int i = actualLength; i < length; i++)
+                               bits[i] = Bit.ZERO;
+               return BitVector.of(bits);
+       }
+
        public BitVectorMutator mutator()
        {
                return BitVectorMutator.of(this);