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 BitVector reverse()
+ {
+ 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);
+ }
}