Convert unsigned long to BigInteger in Java
From the OpenJDK implementation of Long
:
/**
* Return a BigInteger equal to the unsigned value of the argument.
*/
private static BigInteger toUnsignedBigInteger(long i) {
if (i >= 0L) {
return BigInteger.valueOf(i);
} else {
int upper = (int) (i >>> 32);
int lower = (int) i;
// return (upper << 32) + lower
return BigInteger.valueOf(Integer.toUnsignedLong(upper))
.shiftLeft(32)
.add(BigInteger.valueOf(Integer.toUnsignedLong(lower)));
}
}
Comments
Be the first to comment!