Class UnsignedLongs
 java.lang.Object

 com.google.common.primitives.UnsignedLongs

@GwtCompatible public final class UnsignedLongs extends java.lang.Object
Static utility methods pertaining tolong
primitives that interpret values as unsigned (that is, any negative valuex
is treated as the positive value2^64 + x
). The methods for which signedness is not an issue are inLongs
, 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 aString
and aString
to along
that treat thelong
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 theUnsignedLong
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.
 Since:
 10.0
 Author:
 Louis Wasserman, Brian Milch, Colin Evans


Field Summary
Fields Modifier and Type Field Description static long
MAX_VALUE

Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static int
compare(long a, long b)
Compares the two specifiedlong
values, treating them as unsigned values between0
and2^64  1
inclusive.static long
decode(java.lang.String stringValue)
Returns the unsignedlong
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 java.lang.String
join(java.lang.String separator, long... array)
Returns a string containing the supplied unsignedlong
values separated byseparator
.static java.util.Comparator<long[]>
lexicographicalComparator()
Returns a comparator that compares two arrays of unsignedlong
values lexicographically.static long
max(long... array)
Returns the greatest value present inarray
, treating values as unsigned.static long
min(long... array)
Returns the least value present inarray
, treating values as unsigned.static long
parseUnsignedLong(java.lang.String string)
Returns the unsignedlong
value represented by the given decimal string.static long
parseUnsignedLong(java.lang.String string, int radix)
Returns the unsignedlong
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 betweenfromIndex
inclusive andtoIndex
exclusive, treating its elements as unsigned 64bit integers.static void
sortDescending(long[] array)
Sorts the elements ofarray
in descending order, interpreting them as unsigned 64bit integers.static void
sortDescending(long[] array, int fromIndex, int toIndex)
Sorts the elements ofarray
betweenfromIndex
inclusive andtoIndex
exclusive in descending order, interpreting them as unsigned 64bit integers.static java.lang.String
toString(long x)
Returns a string representation of x, where x is treated as unsigned.static java.lang.String
toString(long x, int radix)
Returns a string representation ofx
for the given radix, wherex
is treated as unsigned.



Field Detail

MAX_VALUE
public static final long MAX_VALUE
 See Also:
 Constant Field Values


Method Detail

compare
public static int compare(long a, long b)
Compares the two specifiedlong
values, treating them as unsigned values between0
and2^64  1
inclusive.Java 8+ users: use
Long.compareUnsigned(long, long)
instead. Parameters:
a
 the first unsignedlong
to compareb
 the second unsignedlong
to compare Returns:
 a negative value if
a
is less thanb
; a positive value ifa
is greater thanb
; or zero if they are equal

min
public static long min(long... array)
Returns the least value present inarray
, treating values as unsigned. Parameters:
array
 a nonempty array of unsignedlong
values Returns:
 the value present in
array
that is less than or equal to every other value in the array according tocompare(long, long)
 Throws:
java.lang.IllegalArgumentException
 ifarray
is empty

max
public static long max(long... array)
Returns the greatest value present inarray
, treating values as unsigned. Parameters:
array
 a nonempty array of unsignedlong
values Returns:
 the value present in
array
that is greater than or equal to every other value in the array according tocompare(long, long)
 Throws:
java.lang.IllegalArgumentException
 ifarray
is empty

join
public static java.lang.String join(java.lang.String separator, long... array)
Returns a string containing the supplied unsignedlong
values separated byseparator
. For example,join("", 1, 2, 3)
returns the string"123"
. Parameters:
separator
 the text that should appear between consecutive values in the resulting string (but not at the start or end)array
 an array of unsignedlong
values, possibly empty

lexicographicalComparator
public static java.util.Comparator<long[]> lexicographicalComparator()
Returns a comparator that compares two arrays of unsignedlong
values lexicographically. That is, it compares, usingcompare(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 withArrays.equals(long[], long[])
.

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

sort
public static void sort(long[] array, int fromIndex, int toIndex)
Sorts the array betweenfromIndex
inclusive andtoIndex
exclusive, treating its elements as unsigned 64bit integers. Since:
 23.1

sortDescending
public static void sortDescending(long[] array)
Sorts the elements ofarray
in descending order, interpreting them as unsigned 64bit integers. Since:
 23.1

sortDescending
public static void sortDescending(long[] array, int fromIndex, int toIndex)
Sorts the elements ofarray
betweenfromIndex
inclusive andtoIndex
exclusive in descending order, interpreting them as unsigned 64bit integers. Since:
 23.1

divide
public static long divide(long dividend, long divisor)
Returns dividend / divisor, where the dividend and divisor are treated as unsigned 64bit quantities.Java 8+ users: use
Long.divideUnsigned(long, long)
instead. Parameters:
dividend
 the dividend (numerator)divisor
 the divisor (denominator) Throws:
java.lang.ArithmeticException
 if divisor is 0

remainder
public static long remainder(long dividend, long divisor)
Returns dividend % divisor, where the dividend and divisor are treated as unsigned 64bit quantities.Java 8+ users: use
Long.remainderUnsigned(long, long)
instead. Parameters:
dividend
 the dividend (numerator)divisor
 the divisor (denominator) Throws:
java.lang.ArithmeticException
 if divisor is 0 Since:
 11.0

parseUnsignedLong
@CanIgnoreReturnValue public static long parseUnsignedLong(java.lang.String string)
Returns the unsignedlong
value represented by the given decimal string.Java 8+ users: use
Long.parseUnsignedLong(String)
instead. Throws:
java.lang.NumberFormatException
 if the string does not contain a valid unsignedlong
valuejava.lang.NullPointerException
 ifstring
is null (in contrast toLong.parseLong(String)
)

parseUnsignedLong
@CanIgnoreReturnValue public static long parseUnsignedLong(java.lang.String string, int radix)
Returns the unsignedlong
value represented by a string with the given radix.Java 8+ users: use
Long.parseUnsignedLong(String, int)
instead. Parameters:
string
 the string containing the unsignedlong
representation to be parsed.radix
 the radix to use while parsingstring
 Throws:
java.lang.NumberFormatException
 if the string does not contain a valid unsignedlong
with the given radix, or ifradix
is not betweenCharacter.MIN_RADIX
andCharacter.MAX_RADIX
.java.lang.NullPointerException
 ifstring
is null (in contrast toLong.parseLong(String)
)

decode
@CanIgnoreReturnValue public static long decode(java.lang.String stringValue)
Returns the unsignedlong
value represented by the given string.Accepts a decimal, hexadecimal, or octal number given by specifying the following prefix:
0x
HexDigits0X
HexDigits#
HexDigits0
OctalDigits
 Throws:
java.lang.NumberFormatException
 if the string does not contain a valid unsignedlong
value Since:
 13.0

toString
public static java.lang.String toString(long x)
Returns a string representation of x, where x is treated as unsigned.Java 8+ users: use
Long.toUnsignedString(long)
instead.

toString
public static java.lang.String toString(long x, int radix)
Returns a string representation ofx
for the given radix, wherex
is treated as unsigned.Java 8+ users: use
Long.toUnsignedString(long, int)
instead. Parameters:
x
 the value to convert to a string.radix
 the radix to use while working withx
 Throws:
java.lang.IllegalArgumentException
 ifradix
is not betweenCharacter.MIN_RADIX
andCharacter.MAX_RADIX
.

