My proposal for resolving add92039f433cd315f2087da9c1a0de899927d96
[Mograsim.git] / net.mograsim.logic.core / src / net / mograsim / logic / core / types / BitVector.java
index 7437521..e17ae6e 100644 (file)
@@ -2,6 +2,7 @@ package net.mograsim.logic.core.types;
 
 import static java.lang.String.format;
 
+import java.math.BigInteger;
 import java.util.Arrays;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
@@ -56,6 +57,21 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                return new BitVector(bit.makeArray(length));
        }
 
+       public static BitVector from(long value, int bits)
+       {
+               return from(BigInteger.valueOf(value), bits);
+       }
+
+       public static BitVector from(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);
+       }
+
        public BitVectorMutator mutator()
        {
                return BitVectorMutator.of(this);
@@ -82,6 +98,21 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                return bits.clone();
        }
 
+       /**
+        * Checks if all bits are {@link Bit#isBinary() binary}.
+        * 
+        * @see Bit#isBinary()
+        */
+       public boolean isBinary()
+       {
+               for (int i = 0; i < bits.length; i++)
+               {
+                       if (!bits[i].isBinary())
+                               return false;
+               }
+               return true;
+       }
+
        @Override
        public BitVector join(BitVector t)
        {
@@ -205,12 +236,23 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
 
                /**
                 * Returns an empty mutator which has no bits set and will simply copy the values from the first binary operation performed.
+                * <p>
+                * An empty BitVectorMutator <b>must not</b> be converted to BitVector or used to manipulate single bits until at least one two
+                * operand logic operation is performed.
                 */
                public static BitVectorMutator empty()
                {
                        return new BitVectorMutator(null);
                }
 
+               /**
+                * @see #empty()
+                */
+               public boolean isEmpty()
+               {
+                       return bits == null;
+               }
+
                /**
                 * Produces the resulting, immutable {@link BitVector}<br>
                 * 
@@ -267,6 +309,8 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                 */
                public void setMSBit(int bitIndex, Bit bit)
                {
+                       if (bits == null)
+                               throw new IllegalStateException("cannot set a bit of an empty mutator");
                        bits[bitIndex] = bit;
                }
 
@@ -275,6 +319,8 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                 */
                public void setLSBit(int bitIndex, Bit bit)
                {
+                       if (bits == null)
+                               throw new IllegalStateException("cannot set a bit of an empty mutator");
                        bits[bits.length - bitIndex - 1] = bit;
                }
 
@@ -283,6 +329,8 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                 */
                public Bit getMSBit(int bitIndex)
                {
+                       if (bits == null)
+                               throw new IllegalStateException("cannot get a bit of an empty mutator");
                        return bits[bitIndex];
                }
 
@@ -291,11 +339,15 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                 */
                public Bit getLSBit(int bitIndex)
                {
+                       if (bits == null)
+                               throw new IllegalStateException("cannot get a bit of an empty mutator");
                        return bits[bits.length - bitIndex - 1];
                }
 
                public int length()
                {
+                       if (bits == null)
+                               throw new IllegalStateException("cannot obtain a length of an empty mutator");
                        return bits.length;
                }
 
@@ -360,6 +412,26 @@ public final class BitVector implements StrictLogicType<BitVector>, Iterable<Bit
                return sb.toString();
        }
 
+       /**
+        * Returns the value of the BitVector as BigInteger.
+        * 
+        * @throws NumberFormatException if the BitVector is not {@link #isBinary() binary}.
+        */
+       public BigInteger getUnsignedValue()
+       {
+               if (!isBinary())
+                       throw new NumberFormatException(this + " is not binary");
+               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);
+       }
+
        /**
         * Parses a String containing solely {@link Bit} symbols (MSB first)
         *