return new BitVector(bit.makeArray(length));
}
- public BigInteger getUnsignedValue()
+ public static BitVector from(long value, int bits)
{
- 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);
+ return from(BigInteger.valueOf(value), bits);
}
- 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)
+ 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);
}
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++)
/**
* Does test for equality of values/content, shifting the other BitVector by <code>offset</code> to the right.<br>
- * Therefore <code>offset + other.length() <= this.length()</code> needs to be true.
+ * Therefore <code>offset + other.length() <= this.wdith()</code> needs to be true.
*
* @throws ArrayIndexOutOfBoundsException if <code>offset + other.length() > this.length()</code>
*
}
/**
- * Returns the value of the BitVector as BigInteger either unsigned or as two-complement.
- *
- * @param signed if true and the BitVector represents a negative two-complement integer, an equivalent BigInteger is returned
- * @return the value of this BitVector as BigInteger
+ * Returns the value of the BitVector as BigInteger.
*
+ * @throws NumberFormatException if the BitVector is not {@link #isBinary() binary}.
*/
- public BigInteger toBigInteger(boolean signed)
+ public BigInteger getUnsignedValue()
{
if (!isBinary())
throw new NumberFormatException(this + " is not binary");
- BigInteger val = new BigInteger(toString(), 2);
- if (signed && bits[0] == Bit.ONE)
- val = val.not().setBit(val.bitLength()).add(BigInteger.ONE);
- return val;
+ byte[] bytes = new byte[(bits.length / 8 + (bits.length % 8 == 0 ? 0 : 1)) + 1];
+ for (int i = bits.length - 1; i >= 0; i--)
+ {
+ if (Bit.ONE == bits[bits.length - i - 1])
+ {
+ try
+ {
+ bytes[bytes.length - (i / 8) - 1] |= 1 << (i % 8);
+ }
+ catch (IndexOutOfBoundsException e)
+ {
+ e.printStackTrace();
+ }
+ }
+ }
+ return new BigInteger(bytes);
}
/**
return new BitVector(values);
}
+ /**
+ * Changes a single Bit using the given operation. This can be used to set, clear or flip bits.
+ *
+ * @param msbIndex index of the MSB to be changed
+ * @param singleBitOperation the operation to perform on that Bit
+ * @return the resulting, new BitVektor
+ */
+ public BitVector withBitChanged(int msbIndex, UnaryOperator<Bit> singleBitOperation)
+ {
+ Bit[] newBits = bits.clone();
+ newBits[msbIndex] = singleBitOperation.apply(newBits[msbIndex]);
+ return new BitVector(newBits);
+ }
+
/**
* Iterate over the {@link Bit}s of the BitVector <b>from MSB to LSB</b> (left to right).
*/