Merge branch 'development' of https://gitlab.lrz.de/lrr-tum/students/eragp-misim...
authorFabian Stemmler <stemmler@in.tum.de>
Wed, 28 Aug 2019 19:35:49 +0000 (21:35 +0200)
committerFabian Stemmler <stemmler@in.tum.de>
Wed, 28 Aug 2019 19:35:49 +0000 (21:35 +0200)
net.mograsim.logic.core/src/net/mograsim/logic/core/types/BitVector.java
net.mograsim.logic.core/test/net/mograsim/logic/core/types/BitVectorTest.java

index c223e81..8f53a36 100644 (file)
@@ -420,13 +420,13 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                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)
                                {
@@ -477,4 +477,12 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                        }
                };
        }
+
+       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));
+       }
 }
index 11b5feb..2b46a6c 100644 (file)
@@ -44,9 +44,10 @@ class BitVectorTest
                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