+ public BigInteger getUnsignedValue()
+ {
+ if (!isBinary())
+ throw new NumberFormatException("BitVector is non binary: " + toString());
+ byte[] bytes = new byte[(bits.length / 8) + 1];
+ for (int i = 0; i < bits.length; i++)
+ {
+ if (Bit.ONE == bits[i])
+ {
+ bytes[i / 8] |= 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 static BitVector of(long value, int bits)
+ {
+ return of(BigInteger.valueOf(value), bits);
+ }
+
+ public static BitVector of(BigInteger value, int bits)
+ {
+ 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);
+ }
+