Skip to content

Commit

Permalink
Simplify conversion of numbers to unsigned
Browse files Browse the repository at this point in the history
  • Loading branch information
aherbert committed Dec 24, 2023
1 parent caa41d2 commit a15b3e6
Showing 1 changed file with 6 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
*
*/
class ArithmeticUtilsTest {
/** 2^63. */
private static final BigInteger TWO_POW_63 = BigInteger.ONE.shiftLeft(63);

@Test
void testGcd() {
Expand Down Expand Up @@ -538,7 +540,7 @@ private static long[] getLongSpecialCases() {
}

private static long toUnsignedLong(int number) {
return number < 0 ? 0x100000000L + (long)number : (long)number;
return Integer.toUnsignedLong(number);
}

private static int remainderUnsignedExpected(int dividend, int divisor) {
Expand All @@ -550,7 +552,9 @@ private static int divideUnsignedExpected(int dividend, int divisor) {
}

private static BigInteger toUnsignedBigInteger(long number) {
return number < 0L ? BigInteger.ONE.shiftLeft(64).add(BigInteger.valueOf(number)) : BigInteger.valueOf(number);
return number < 0 ?
TWO_POW_63.or(BigInteger.valueOf(number & Long.MAX_VALUE)) :
BigInteger.valueOf(number);
}

private static long remainderUnsignedExpected(long dividend, long divisor) {
Expand Down

0 comments on commit a15b3e6

Please sign in to comment.