if (!isBinary())
throw new NumberFormatException(this + " is not binary");
byte[] bytes = new byte[(bits.length / 8 + (bits.length % 8 == 0 ? 0 : 1)) + 1];
- for (int i = 0; i < bits.length; i++)
+ for (int i = bits.length - 1; i >= 0; i--)
{
- if (Bit.ONE == bits[i])
+ if (Bit.ONE == bits[bits.length - i - 1])
{
try
{
- bytes[(i / 8) + 1] |= 1 << (i % 8);
+ bytes[bytes.length - (i / 8) - 1] |= 1 << (i % 8);
}
catch (IndexOutOfBoundsException e)
{
}
};
}
+
+ public static void main(String[] args)
+ {
+// System.out.println(new BigInteger(new byte[] { 0b1000000, 0b00000000 }).toString(2));
+// System.out.println(BitVector.SINGLE_1.concat(BitVector.of(Bit.ZERO, 14)).getUnsignedValue().toString(2));
+ System.out.println(new BigInteger(new byte[] { 0b0000000, 0b00000101 }).toString(2));
+ System.out.println(BitVector.of(Bit.ZERO, 7).concat(BitVector.of(Bit.ONE, Bit.ZERO, Bit.ONE)).getUnsignedValue().toString(2));
+ }
}
assertEquals(BigInteger.valueOf(0b101), BitVector.parse("101").getUnsignedValue());
assertEquals(BigInteger.valueOf(0b01010), BitVector.parse("01010").getUnsignedValue());
assertEquals(BigInteger.valueOf(0), BitVector.parse("0000").getUnsignedValue());
+ assertEquals(BigInteger.valueOf(0b0000000101), BitVector.parse("0000000101").getUnsignedValue());
+ assertEquals(BigInteger.valueOf(0b1010000000), BitVector.parse("1010000000").getUnsignedValue());
assertThrows(NumberFormatException.class, () -> BitVector.parse("00X1").getUnsignedValue());
-
}
@Test