Top Description Inners Fields Constructors Methods
com.google.common.primitives

public final Class UnsignedInts

extends Object
Class Inheritance
Annotations
@GwtCompatible
@ElementTypesAreNonnullByDefault
Static Imports
com.google.common.base.Preconditions.checkArgument, .Preconditions.checkNotNull, .Preconditions.checkPositionIndexes

Static utility methods pertaining to int primitives that interpret values as unsigned (that is, any negative value x is treated as the positive value 2^32 + x). The methods for which signedness is not an issue are in Ints, as well as signed versions of methods for which signedness is an issue.

In addition, this class provides several static methods for converting an int to a String and a String to an int that treat the int as an unsigned number.

Users of these utilities must be extremely careful not to mix up signed and unsigned int values. When possible, it is recommended that the UnsignedInteger 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.

Author
Louis Wasserman
Since
11.0

Nested and Inner Type Summary

Modifier and TypeClass and Description
pack-priv static enum

Field Summary

Modifier and TypeField and Description
pack-priv static final long

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public static int

Returns:

the int value that, when treated as unsigned, equals value
checkedCast
(long
a value between 0 and 232-1 inclusive
value
)

Returns the int value that, when treated as unsigned, is equal to value, if possible.

public static int

Returns:

a negative value if a is less than b; a positive value if a is greater than b; or zero if they are equal
compare
(int
the first unsigned int to compare
a
,
int
the second unsigned int to compare
b
)

Compares the two specified int values, treating them as unsigned values between 0 and 2^32 - 1 inclusive.

public static int
decode(String stringValue)

Returns the unsigned int value represented by the given string.

public static int
divide(int
the dividend (numerator)
dividend
,
int
the divisor (denominator)
divisor
)

Returns dividend / divisor, where the dividend and divisor are treated as unsigned 32-bit quantities.

pack-priv static int
flip(int value)

public static String
join(String
the text that should appear between consecutive values in the resulting string (but not at the start or end)
separator
,
int...
an array of unsigned int values, possibly empty
array
)

Returns a string containing the supplied unsigned int values separated by separator.

public static Comparator<int[]>
lexicographicalComparator()

Returns a comparator that compares two arrays of unsigned int values lexicographically.

public static int

Returns:

the value present in array that is greater than or equal to every other value in the array according to compare
max
(int...
a nonempty array of unsigned int values
array
)

Returns the greatest value present in array, treating values as unsigned.

public static int

Returns:

the value present in array that is less than or equal to every other value in the array according to compare
min
(int...
a nonempty array of unsigned int values
array
)

Returns the least value present in array, treating values as unsigned.

public static int
parseUnsignedInt(String s)

Returns the unsigned int value represented by the given decimal string.

public static int
parseUnsignedInt(String
the string containing the unsigned integer representation to be parsed.
string
,
int
the radix to use while parsing s; must be between Character#MIN_RADIX and Character#MAX_RADIX.
radix
)

Returns the unsigned int value represented by a string with the given radix.

public static int
remainder(int
the dividend (numerator)
dividend
,
int
the divisor (denominator)
divisor
)

Returns dividend % divisor, where the dividend and divisor are treated as unsigned 32-bit quantities.

public static int

Returns:

2^32 - 1 if value >= 2^32, 0 if value <= 0, and value cast to int otherwise
saturatedCast
(long
any long value
value
)

Returns the int value that, when treated as unsigned, is nearest in value to value.

public static void
sort(int[] array)

Sorts the array, treating its elements as unsigned 32-bit integers.

public static void
sort(int[] array, int fromIndex, int toIndex)

Sorts the array between fromIndex inclusive and toIndex exclusive, treating its elements as unsigned 32-bit integers.

public static void
sortDescending(int[] array)

Sorts the elements of array in descending order, interpreting them as unsigned 32-bit integers.

public static void
sortDescending(int[] array, int fromIndex, int toIndex)

Sorts the elements of array between fromIndex inclusive and toIndex exclusive in descending order, interpreting them as unsigned 32-bit integers.

public static long
toLong(int value)

Returns the value of the given int as a long, when treated as unsigned.

public static String
toString(int x)

Returns a string representation of x, where x is treated as unsigned.

public static String
toString(int
the value to convert to a string.
x
,
int
the radix to use while working with x
radix
)

Returns a string representation of x for the given radix, where x is treated as unsigned.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

INT_MASKback to summary
pack-priv static final long INT_MASK

Constructor Detail

UnsignedIntsback to summary
private UnsignedInts()

Method Detail

checkedCastback to summary
public static int checkedCast(long value)

Returns the int value that, when treated as unsigned, is equal to value, if possible.

Parameters
value:long

a value between 0 and 232-1 inclusive

Returns:int

the int value that, when treated as unsigned, equals value

Exceptions
IllegalArgumentException:
if value is negative or greater than or equal to 232
Since
21.0
compareback to summary
public static int compare(int a, int b)

Compares the two specified int values, treating them as unsigned values between 0 and 2^32 - 1 inclusive.

Java 8+ users: use Integer#compareUnsigned(int, int) instead.

Parameters
a:int

the first unsigned int to compare

b:int

the second unsigned int to compare

Returns:int

a negative value if a is less than b; a positive value if a is greater than b; or zero if they are equal

decodeback to summary
public static int decode(String stringValue)

Returns the unsigned int value represented by the given string.

Accepts a decimal, hexadecimal, or octal number given by specifying the following prefix:

  • 0xHexDigits
  • 0XHexDigits
  • #HexDigits
  • 0OctalDigits
Annotations
@CanIgnoreReturnValue
Exceptions
NumberFormatException:
if the string does not contain a valid unsigned int value
Since
13.0
divideback to summary
public static int divide(int dividend, int divisor)

Returns dividend / divisor, where the dividend and divisor are treated as unsigned 32-bit quantities.

Java 8+ users: use Integer#divideUnsigned(int, int) instead.

Parameters
dividend:int

the dividend (numerator)

divisor:int

the divisor (denominator)

Exceptions
ArithmeticException:
if divisor is 0
flipback to summary
pack-priv static int flip(int value)
joinback to summary
public static String join(String separator, int... array)

Returns a string containing the supplied unsigned int values separated by separator. For example, join("-", 1, 2, 3) returns the string "1-2-3".

Parameters
separator:String

the text that should appear between consecutive values in the resulting string (but not at the start or end)

array:int[]

an array of unsigned int values, possibly empty

lexicographicalComparatorback to summary
public static Comparator<int[]> lexicographicalComparator()

Returns a comparator that compares two arrays of unsigned int values lexicographically. That is, it compares, using compare(int, int)), 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, [] < [1] < [1, 2] < [2] < [1 << 31].

The returned comparator is inconsistent with Object#equals(Object) (since arrays support only identity equality), but it is consistent with Arrays#equals(int[], int[]).

maxback to summary
public static int max(int... array)

Returns the greatest value present in array, treating values as unsigned.

Parameters
array:int[]

a nonempty array of unsigned int values

Returns:int

the value present in array that is greater than or equal to every other value in the array according to compare

Exceptions
IllegalArgumentException:
if array is empty
minback to summary
public static int min(int... array)

Returns the least value present in array, treating values as unsigned.

Parameters
array:int[]

a nonempty array of unsigned int values

Returns:int

the value present in array that is less than or equal to every other value in the array according to compare

Exceptions
IllegalArgumentException:
if array is empty
parseUnsignedIntback to summary
public static int parseUnsignedInt(String s)

Returns the unsigned int value represented by the given decimal string.

Java 8+ users: use Integer#parseUnsignedInt(String) instead.

Annotations
@CanIgnoreReturnValue
Exceptions
NumberFormatException:
if the string does not contain a valid unsigned int value
NullPointerException:
if s is null (in contrast to Integer#parseInt(String))
parseUnsignedIntback to summary
public static int parseUnsignedInt(String string, int radix)

Returns the unsigned int value represented by a string with the given radix.

Java 8+ users: use Integer#parseUnsignedInt(String, int) instead.

Parameters
string:String

the string containing the unsigned integer representation to be parsed.

radix:int

the radix to use while parsing s; must be between Character#MIN_RADIX and Character#MAX_RADIX.

Annotations
@CanIgnoreReturnValue
Exceptions
NumberFormatException:
if the string does not contain a valid unsigned int, or if supplied radix is invalid.
NullPointerException:
if s is null (in contrast to Integer#parseInt(String))
remainderback to summary
public static int remainder(int dividend, int divisor)

Returns dividend % divisor, where the dividend and divisor are treated as unsigned 32-bit quantities.

Java 8+ users: use Integer#remainderUnsigned(int, int) instead.

Parameters
dividend:int

the dividend (numerator)

divisor:int

the divisor (denominator)

Exceptions
ArithmeticException:
if divisor is 0
saturatedCastback to summary
public static int saturatedCast(long value)

Returns the int value that, when treated as unsigned, is nearest in value to value.

Parameters
value:long

any long value

Returns:int

2^32 - 1 if value >= 2^32, 0 if value <= 0, and value cast to int otherwise

Since
21.0
sortback to summary
public static void sort(int[] array)

Sorts the array, treating its elements as unsigned 32-bit integers.

Since
23.1
sortback to summary
public static void sort(int[] array, int fromIndex, int toIndex)

Sorts the array between fromIndex inclusive and toIndex exclusive, treating its elements as unsigned 32-bit integers.

Since
23.1
sortDescendingback to summary
public static void sortDescending(int[] array)

Sorts the elements of array in descending order, interpreting them as unsigned 32-bit integers.

Since
23.1
sortDescendingback to summary
public static void sortDescending(int[] array, int fromIndex, int toIndex)

Sorts the elements of array between fromIndex inclusive and toIndex exclusive in descending order, interpreting them as unsigned 32-bit integers.

Since
23.1
toLongback to summary
public static long toLong(int value)

Returns the value of the given int as a long, when treated as unsigned.

Java 8+ users: use Integer#toUnsignedLong(int) instead.

toStringback to summary
public static String toString(int x)

Returns a string representation of x, where x is treated as unsigned.

Java 8+ users: use Integer#toUnsignedString(int) instead.

toStringback to summary
public static String toString(int x, int radix)

Returns a string representation of x for the given radix, where x is treated as unsigned.

Java 8+ users: use Integer#toUnsignedString(int, int) instead.

Parameters
x:int

the value to convert to a string.

radix:int

the radix to use while working with x

Exceptions
IllegalArgumentException:
if radix is not between Character#MIN_RADIX and Character#MAX_RADIX.
com.google.common.primitives back to summary

pack-priv final Enum UnsignedInts.LexicographicalComparator

extends Enum<UnsignedInts.LexicographicalComparator>
implements Comparator<int[]>
Class Inheritance
All Implemented Interfaces
java.util.Comparator

Field Summary

Modifier and TypeField and Description
public static final UnsignedInts.LexicographicalComparator

Constructor Summary

AccessConstructor and Description
private

Method Summary

Modifier and TypeMethod and Description
public int
compare(int[]
the first object to be compared.
left
,
int[]
the second object to be compared.
right
)

Implements java.util.Comparator.compare.

Compares its two arguments for order.
public String
toString()

Overrides java.lang.Enum.toString.

Returns the name of this enum constant, as contained in the declaration.
public static UnsignedInts.LexicographicalComparator
public static UnsignedInts.LexicographicalComparator[]
Inherited from java.lang.Enum:
clonecompareTodescribeConstableequalsfinalizegetDeclaringClasshashCodenameordinalvalueOf

Field Detail

INSTANCEback to summary
public static final UnsignedInts.LexicographicalComparator INSTANCE

Constructor Detail

LexicographicalComparatorback to summary
private LexicographicalComparator()

Method Detail

compareback to summary
public int compare(int[] left, int[] right)

Implements java.util.Comparator.compare.

Doc from java.util.Comparator.compare.

Compares its two arguments for order. Returns a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

The implementor must ensure that signum(compare(x, y)) == -signum(compare(y, x)) for all x and y. (This implies that compare(x, y) must throw an exception if and only if compare(y, x) throws an exception.)

The implementor must also ensure that the relation is transitive: ((compare(x, y)>0) && (compare(y, z)>0)) implies compare(x, z)>0.

Finally, the implementor must ensure that compare(x, y)==0 implies that signum(compare(x, z))==signum(compare(y, z)) for all z.

Parameters
left:int[]

the first object to be compared.

right:int[]

the second object to be compared.

Returns:int

a negative integer, zero, or a positive integer as the first argument is less than, equal to, or greater than the second.

Annotations
@Override
toStringback to summary
public String toString()

Overrides java.lang.Enum.toString.

Doc from java.lang.Enum.toString.

Returns the name of this enum constant, as contained in the declaration. This method may be overridden, though it typically isn't necessary or desirable. An enum class should override this method when a more "programmer-friendly" string form exists.

Returns:String

the name of this enum constant

Annotations
@Override
valueOfback to summary
public static UnsignedInts.LexicographicalComparator valueOf(String name)
valuesback to summary
public static UnsignedInts.LexicographicalComparator[] values()