return new BigInteger(bytes);
}
+ public long getUnsignedValueLong()
+ {
+ return getUnsignedValue().longValue();
+ }
+
+ /**
+ * Returns the value of the BitVector as BigInteger interpreted as a two's complement number.
+ *
+ * @throws NumberFormatException if the BitVector is not {@link #isBinary() binary}.
+ *
+ * @author Daniel Kirschten
+ */
+ public BigInteger getSignedValue()
+ {
+ BigInteger unsignedValue = getUnsignedValue();
+ if (bits[bits.length - 1] == Bit.ZERO)
+ return unsignedValue;
+ return unsignedValue.subtract(BitVector.of(Bit.ONE, bits.length).getUnsignedValue()).subtract(BigInteger.ONE);// TODO speed this up!
+ }
+
+ public long getSignedValueLong()
+ {
+ return getSignedValue().longValue();
+ }
+
/**
* Parses a String containing solely {@link Bit} symbols (MSB first)
*
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).
*/
};
}
- public static void main(String[] args)
+ public BitVector reverse()
{
-// 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));
+ int length = length();
+ Bit[] other = new Bit[length];
+ for (int i = 0, j = length - 1; i < length; i++, j--)
+ {
+ other[i] = bits[j];
+ }
+ return new BitVector(other);
}
}