@Beta @GwtCompatible public final class UnsignedLongs extends Object
long
primitives that interpret values as
unsigned (that is, any negative value x
is treated as the positive value 2^64 + x
). The methods for which signedness is not an issue are in Longs
, as well as
signed versions of methods for which signedness is an issue.
In addition, this class provides several static methods for converting a long
to a
String
and a String
to a long
that treat the long
as an unsigned
number.
Users of these utilities must be extremely careful not to mix up signed and unsigned
long
values. When possible, it is recommended that the UnsignedLong
wrapper class
be used, at a small efficiency penalty, to enforce the distinction in the type system.
See the Guava User Guide article on unsigned primitive utilities.
Modifier and Type  Field and Description 

static long 
MAX_VALUE 
Modifier and Type  Method and Description 

static int 
compare(long a,
long b)
Compares the two specified
long values, treating them as unsigned values between 0 and 2^64  1 inclusive. 
static long 
decode(String stringValue)
Returns the unsigned
long value represented by the given string. 
static long 
divide(long dividend,
long divisor)
Returns dividend / divisor, where the dividend and divisor are treated as unsigned 64bit
quantities.

static String 
join(String separator,
long... array)
Returns a string containing the supplied unsigned
long values separated by separator . 
static Comparator<long[]> 
lexicographicalComparator()
Returns a comparator that compares two arrays of unsigned
long values lexicographically. 
static long 
max(long... array)
Returns the greatest value present in
array , treating values as unsigned. 
static long 
min(long... array)
Returns the least value present in
array , treating values as unsigned. 
static long 
parseUnsignedLong(String string)
Returns the unsigned
long value represented by the given decimal string. 
static long 
parseUnsignedLong(String string,
int radix)
Returns the unsigned
long value represented by a string with the given radix. 
static long 
remainder(long dividend,
long divisor)
Returns dividend % divisor, where the dividend and divisor are treated as unsigned 64bit
quantities.

static void 
sort(long[] array)
Sorts the array, treating its elements as unsigned 64bit integers.

static void 
sort(long[] array,
int fromIndex,
int toIndex)
Sorts the array between
fromIndex inclusive and toIndex exclusive, treating its
elements as unsigned 64bit integers. 
static void 
sortDescending(long[] array)
Sorts the elements of
array in descending order, interpreting them as unsigned 64bit
integers. 
static void 
sortDescending(long[] array,
int fromIndex,
int toIndex)
Sorts the elements of
array between fromIndex inclusive and toIndex
exclusive in descending order, interpreting them as unsigned 64bit integers. 
static String 
toString(long x)
Returns a string representation of x, where x is treated as unsigned.

static String 
toString(long x,
int radix)
Returns a string representation of
x for the given radix, where x is treated as
unsigned. 
public static final long MAX_VALUE
public static int compare(long a, long b)
long
values, treating them as unsigned values between 0
and 2^64  1
inclusive.
Java 8 users: use Long.compareUnsigned(long, long)
instead.
a
 the first unsigned long
to compareb
 the second unsigned long
to comparea
is less than b
; a positive value if a
is
greater than b
; or zero if they are equalpublic static long min(long... array)
array
, treating values as unsigned.array
 a nonempty array of unsigned long
valuesarray
that is less than or equal to every other value in
the array according to compare(long, long)
IllegalArgumentException
 if array
is emptypublic static long max(long... array)
array
, treating values as unsigned.array
 a nonempty array of unsigned long
valuesarray
that is greater than or equal to every other value
in the array according to compare(long, long)
IllegalArgumentException
 if array
is emptypublic static String join(String separator, long... array)
long
values separated by separator
. For example, join("", 1, 2, 3)
returns the string "123"
.separator
 the text that should appear between consecutive values in the resulting string
(but not at the start or end)array
 an array of unsigned long
values, possibly emptypublic static Comparator<long[]> lexicographicalComparator()
long
values lexicographically. That is, it
compares, using compare(long, long)
), the first pair of values that follow any common
prefix, or when one array is a prefix of the other, treats the shorter array as the lesser. For
example, [] < [1L] < [1L, 2L] < [2L] < [1L << 63]
.
The returned comparator is inconsistent with Object.equals(Object)
(since arrays
support only identity equality), but it is consistent with Arrays.equals(long[],
long[])
.
public static void sort(long[] array)
public static void sort(long[] array, int fromIndex, int toIndex)
fromIndex
inclusive and toIndex
exclusive, treating its
elements as unsigned 64bit integers.public static void sortDescending(long[] array)
array
in descending order, interpreting them as unsigned 64bit
integers.public static void sortDescending(long[] array, int fromIndex, int toIndex)
array
between fromIndex
inclusive and toIndex
exclusive in descending order, interpreting them as unsigned 64bit integers.public static long divide(long dividend, long divisor)
Java 8 users: use Long.divideUnsigned(long, long)
instead.
dividend
 the dividend (numerator)divisor
 the divisor (denominator)ArithmeticException
 if divisor is 0public static long remainder(long dividend, long divisor)
Java 8 users: use Long.remainderUnsigned(long, long)
instead.
dividend
 the dividend (numerator)divisor
 the divisor (denominator)ArithmeticException
 if divisor is 0@CanIgnoreReturnValue public static long parseUnsignedLong(String string)
long
value represented by the given decimal string.
Java 8 users: use Long.parseUnsignedLong(String)
instead.
NumberFormatException
 if the string does not contain a valid unsigned long
valueNullPointerException
 if string
is null (in contrast to Long.parseLong(String)
)@CanIgnoreReturnValue public static long parseUnsignedLong(String string, int radix)
long
value represented by a string with the given radix.
Java 8 users: use Long.parseUnsignedLong(String, int)
instead.
string
 the string containing the unsigned long
representation to be parsed.radix
 the radix to use while parsing string
NumberFormatException
 if the string does not contain a valid unsigned long
with
the given radix, or if radix
is not between Character.MIN_RADIX
and Character.MAX_RADIX
.NullPointerException
 if string
is null (in contrast to Long.parseLong(String)
)@CanIgnoreReturnValue public static long decode(String stringValue)
long
value represented by the given string.
Accepts a decimal, hexadecimal, or octal number given by specifying the following prefix:
0x
HexDigits
0X
HexDigits
#
HexDigits
0
OctalDigits
NumberFormatException
 if the string does not contain a valid unsigned long
valuepublic static String toString(long x)
Java 8 users: use Long.toUnsignedString(long)
instead.
public static String toString(long x, int radix)
x
for the given radix, where x
is treated as
unsigned.
Java 8 users: use Long.toUnsignedString(long, int)
instead.
x
 the value to convert to a string.radix
 the radix to use while working with x
IllegalArgumentException
 if radix
is not between Character.MIN_RADIX
and Character.MAX_RADIX
.Copyright © 2010–2019. All rights reserved.