Refine search
@Override public int compareTo(UnsignedLong o) { checkNotNull(o); return UnsignedLongs.compare(value, o.value); }
/** * Returns a string representation of x, where x is treated as unsigned. * * <p><b>Java 8 users:</b> use {@link Long#toUnsignedString(long)} instead. */ public static String toString(long x) { return toString(x, 10); }
/** * Returns an {@code UnsignedLong} holding the value of the specified {@code String}, parsed as an * unsigned {@code long} value in the specified radix. * * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} * value, or {@code radix} is not between {@link Character#MIN_RADIX} and {@link * Character#MAX_RADIX} */ @CanIgnoreReturnValue public static UnsignedLong valueOf(String string, int radix) { return fromLongBits(UnsignedLongs.parseUnsignedLong(string, radix)); }
/** * Returns the result of dividing this by {@code val}. * * @since 14.0 */ public UnsignedLong dividedBy(UnsignedLong val) { return fromLongBits(UnsignedLongs.divide(value, checkNotNull(val).value)); }
/** * Returns this modulo {@code val}. * * @since 14.0 */ public UnsignedLong mod(UnsignedLong val) { return fromLongBits(UnsignedLongs.remainder(value, checkNotNull(val).value)); }
public void testAsUnsignedAndLongValueAreInverses() { for (long value : TEST_LONGS) { assertEquals( UnsignedLongs.toString(value), value, UnsignedLong.fromLongBits(value).longValue()); } }
/** * Returns the unsigned {@code long} value represented by the given decimal string. * * <p><b>Java 8 users:</b> use {@link Long#parseUnsignedLong(String)} instead. * * @throws NumberFormatException if the string does not contain a valid unsigned {@code long} * value * @throws NullPointerException if {@code string} is null (in contrast to {@link * Long#parseLong(String)}) */ @CanIgnoreReturnValue public static long parseUnsignedLong(String string) { return parseUnsignedLong(string, 10); }
public void testAsUnsignedBigIntegerValue() { for (long value : TEST_LONGS) { BigInteger expected = (value >= 0) ? BigInteger.valueOf(value) : BigInteger.valueOf(value).add(BigInteger.ZERO.setBit(64)); assertEquals( UnsignedLongs.toString(value), expected, UnsignedLong.fromLongBits(value).bigIntegerValue()); } }
/** * Returns the least value present in {@code array}, treating values as unsigned. * * @param array a <i>nonempty</i> array of unsigned {@code long} values * @return the value present in {@code array} that is less than or equal to every other value in * the array according to {@link #compare} * @throws IllegalArgumentException if {@code array} is empty */ public static long min(long... array) { checkArgument(array.length > 0); long min = flip(array[0]); for (int i = 1; i < array.length; i++) { long next = flip(array[i]); if (next < min) { min = next; } } return flip(min); }
/** Returns (a * 2^32) mod m. a may be any unsigned long. */ private long times2ToThe32Mod(long a, long m) { int remainingPowersOf2 = 32; do { int shift = Math.min(remainingPowersOf2, Long.numberOfLeadingZeros(a)); // shift is either the number of powers of 2 left to multiply a by, or the biggest shift // possible while keeping a in an unsigned long. a = UnsignedLongs.remainder(a << shift, m); remainingPowersOf2 -= shift; } while (remainingPowersOf2 > 0); return a; }
@GwtIncompatible // Too slow in GWT (~3min fully optimized) public void testDivideRemainderEuclideanProperty() { // Use a seed so that the test is deterministic: Random r = new Random(0L); for (int i = 0; i < 1000000; i++) { long dividend = r.nextLong(); long divisor = r.nextLong(); // Test that the Euclidean property is preserved: assertEquals( 0, dividend - (divisor * UnsignedLongs.divide(dividend, divisor) + UnsignedLongs.remainder(dividend, divisor))); } }
quotient = divide(x, radix);
/** * Returns an {@code UnsignedLong} holding the value of the specified {@code String}, parsed as * an unsigned {@code long} value in the specified radix. * * @throws NumberFormatException if the string does not contain a parsable unsigned {@code long} * value, or {@code radix} is not between {@link Character#MIN_RADIX} and * {@link Character#MAX_RADIX} */ public static UnsignedLong valueOf(String string, int radix) { return asUnsigned(UnsignedLongs.parseUnsignedLong(string, radix)); }
/** * Returns the remainder of dividing this by {@code val}. */ public UnsignedLong remainder(UnsignedLong val) { checkNotNull(val); return asUnsigned(UnsignedLongs.remainder(value, val.value)); }
/** * Returns the result of dividing this by {@code val}. */ public UnsignedLong divide(UnsignedLong val) { checkNotNull(val); return asUnsigned(UnsignedLongs.divide(value, val.value)); }
/** * Returns the result of dividing this by {@code val}. * * @since 14.0 */ public UnsignedLong dividedBy(UnsignedLong val) { return fromLongBits(UnsignedLongs.divide(value, checkNotNull(val).value)); }
/** * Returns this modulo {@code val}. * * @since 14.0 */ public UnsignedLong mod(UnsignedLong val) { return fromLongBits(UnsignedLongs.remainder(value, checkNotNull(val).value)); }
public void testAsUnsignedAndLongValueAreInverses() { for (long value : TEST_LONGS) { assertEquals( UnsignedLongs.toString(value), value, UnsignedLong.fromLongBits(value).longValue()); } }
/** * Returns the unsigned {@code long} value represented by the given decimal string. * * <p><b>Java 8 users:</b> use {@link Long#parseUnsignedLong(String)} instead. * * @throws NumberFormatException if the string does not contain a valid unsigned {@code long} * value * @throws NullPointerException if {@code string} is null (in contrast to {@link * Long#parseLong(String)}) */ @CanIgnoreReturnValue public static long parseUnsignedLong(String string) { return parseUnsignedLong(string, 10); }
/** * Returns the greatest value present in {@code array}, treating values as unsigned. * * @param array a <i>nonempty</i> array of unsigned {@code long} values * @return the value present in {@code array} that is greater than or equal to every other value * in the array according to {@link #compare} * @throws IllegalArgumentException if {@code array} is empty */ public static long max(long... array) { checkArgument(array.length > 0); long max = flip(array[0]); for (int i = 1; i < array.length; i++) { long next = flip(array[i]); if (next > max) { max = next; } } return flip(max); }