Class UnsignedInts
- java.lang.Object
- 
- com.google.common.primitives.UnsignedInts
 
- 
 @GwtCompatible public final class UnsignedInts extends java.lang.Object Static utility methods pertaining tointprimitives that interpret values as unsigned (that is, any negative valuexis treated as the positive value2^32 + x). The methods for which signedness is not an issue are inInts, as well as signed versions of methods for which signedness is an issue.In addition, this class provides several static methods for converting an intto aStringand aStringto anintthat treat theintas an unsigned number.Users of these utilities must be extremely careful not to mix up signed and unsigned intvalues. When possible, it is recommended that theUnsignedIntegerwrapper 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:
- 11.0
- Author:
- Louis Wasserman
 
- 
- 
Method SummaryAll Methods Static Methods Concrete Methods Modifier and Type Method Description static intcheckedCast(long value)Returns theintvalue that, when treated as unsigned, is equal tovalue, if possible.static intcompare(int a, int b)Compares the two specifiedintvalues, treating them as unsigned values between0and2^32 - 1inclusive.static intdecode(java.lang.String stringValue)Returns the unsignedintvalue represented by the given string.static intdivide(int dividend, int divisor)Returns dividend / divisor, where the dividend and divisor are treated as unsigned 32-bit quantities.static java.lang.Stringjoin(java.lang.String separator, int... array)Returns a string containing the supplied unsignedintvalues separated byseparator.static java.util.Comparator<int[]>lexicographicalComparator()Returns a comparator that compares two arrays of unsignedintvalues lexicographically.static intmax(int... array)Returns the greatest value present inarray, treating values as unsigned.static intmin(int... array)Returns the least value present inarray, treating values as unsigned.static intparseUnsignedInt(java.lang.String s)Returns the unsignedintvalue represented by the given decimal string.static intparseUnsignedInt(java.lang.String string, int radix)Returns the unsignedintvalue represented by a string with the given radix.static intremainder(int dividend, int divisor)Returns dividend % divisor, where the dividend and divisor are treated as unsigned 32-bit quantities.static intsaturatedCast(long value)Returns theintvalue that, when treated as unsigned, is nearest in value tovalue.static voidsort(int[] array)Sorts the array, treating its elements as unsigned 32-bit integers.static voidsort(int[] array, int fromIndex, int toIndex)Sorts the array betweenfromIndexinclusive andtoIndexexclusive, treating its elements as unsigned 32-bit integers.static voidsortDescending(int[] array)Sorts the elements ofarrayin descending order, interpreting them as unsigned 32-bit integers.static voidsortDescending(int[] array, int fromIndex, int toIndex)Sorts the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive in descending order, interpreting them as unsigned 32-bit integers.static longtoLong(int value)Returns the value of the givenintas along, when treated as unsigned.static java.lang.StringtoString(int x)Returns a string representation of x, where x is treated as unsigned.static java.lang.StringtoString(int x, int radix)Returns a string representation ofxfor the given radix, wherexis treated as unsigned.
 
- 
- 
- 
Method Detail- 
comparepublic static int compare(int a, int b) Compares the two specifiedintvalues, treating them as unsigned values between0and2^32 - 1inclusive.Java 8 users: use Integer.compareUnsigned(int, int)instead.- Parameters:
- a- the first unsigned- intto compare
- b- the second unsigned- intto compare
- Returns:
- a negative value if ais less thanb; a positive value ifais greater thanb; or zero if they are equal
 
 - 
toLongpublic static long toLong(int value) Returns the value of the givenintas along, when treated as unsigned.Java 8 users: use Integer.toUnsignedLong(int)instead.
 - 
checkedCastpublic static int checkedCast(long value) Returns theintvalue that, when treated as unsigned, is equal tovalue, if possible.- Parameters:
- value- a value between 0 and 232-1 inclusive
- Returns:
- the intvalue that, when treated as unsigned, equalsvalue
- Throws:
- java.lang.IllegalArgumentException- if- valueis negative or greater than or equal to 232
- Since:
- 21.0
 
 - 
saturatedCastpublic static int saturatedCast(long value) Returns theintvalue that, when treated as unsigned, is nearest in value tovalue.- Parameters:
- value- any- longvalue
- Returns:
- 2^32 - 1if- value >= 2^32,- 0if- value <= 0, and- valuecast to- intotherwise
- Since:
- 21.0
 
 - 
minpublic static int min(int... array) Returns the least value present inarray, treating values as unsigned.- Parameters:
- array- a nonempty array of unsigned- intvalues
- Returns:
- the value present in arraythat is less than or equal to every other value in the array according tocompare(int, int)
- Throws:
- java.lang.IllegalArgumentException- if- arrayis empty
 
 - 
maxpublic static int max(int... array) Returns the greatest value present inarray, treating values as unsigned.- Parameters:
- array- a nonempty array of unsigned- intvalues
- Returns:
- the value present in arraythat is greater than or equal to every other value in the array according tocompare(int, int)
- Throws:
- java.lang.IllegalArgumentException- if- arrayis empty
 
 - 
joinpublic static java.lang.String join(java.lang.String separator, int... array) Returns a string containing the supplied unsignedintvalues separated byseparator. For example,join("-", 1, 2, 3)returns the string"1-2-3".- 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 unsigned- intvalues, possibly empty
 
 - 
lexicographicalComparatorpublic static java.util.Comparator<int[]> lexicographicalComparator() Returns a comparator that compares two arrays of unsignedintvalues lexicographically. That is, it compares, usingcompare(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 withArrays.equals(int[], int[]).
 - 
sortpublic static void sort(int[] array) Sorts the array, treating its elements as unsigned 32-bit integers.- Since:
- 23.1
 
 - 
sortpublic static void sort(int[] array, int fromIndex, int toIndex) Sorts the array betweenfromIndexinclusive andtoIndexexclusive, treating its elements as unsigned 32-bit integers.- Since:
- 23.1
 
 - 
sortDescendingpublic static void sortDescending(int[] array) Sorts the elements ofarrayin descending order, interpreting them as unsigned 32-bit integers.- Since:
- 23.1
 
 - 
sortDescendingpublic static void sortDescending(int[] array, int fromIndex, int toIndex) Sorts the elements ofarraybetweenfromIndexinclusive andtoIndexexclusive in descending order, interpreting them as unsigned 32-bit integers.- Since:
- 23.1
 
 - 
dividepublic 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- the dividend (numerator)
- divisor- the divisor (denominator)
- Throws:
- java.lang.ArithmeticException- if divisor is 0
 
 - 
remainderpublic 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- the dividend (numerator)
- divisor- the divisor (denominator)
- Throws:
- java.lang.ArithmeticException- if divisor is 0
 
 - 
decode@CanIgnoreReturnValue public static int decode(java.lang.String stringValue) Returns the unsignedintvalue represented by the given string.Accepts a decimal, hexadecimal, or octal number given by specifying the following prefix: - 0xHexDigits
- 0XHexDigits
- #HexDigits
- 0OctalDigits
 - Throws:
- java.lang.NumberFormatException- if the string does not contain a valid unsigned- intvalue
- Since:
- 13.0
 
 - 
parseUnsignedInt@CanIgnoreReturnValue public static int parseUnsignedInt(java.lang.String s) Returns the unsignedintvalue represented by the given decimal string.Java 8 users: use Integer.parseUnsignedInt(String)instead.- Throws:
- java.lang.NumberFormatException- if the string does not contain a valid unsigned- intvalue
- java.lang.NullPointerException- if- sis null (in contrast to- Integer.parseInt(String))
 
 - 
parseUnsignedInt@CanIgnoreReturnValue public static int parseUnsignedInt(java.lang.String string, int radix) Returns the unsignedintvalue represented by a string with the given radix.Java 8 users: use Integer.parseUnsignedInt(String, int)instead.- Parameters:
- string- the string containing the unsigned integer representation to be parsed.
- radix- the radix to use while parsing- s; must be between- Character.MIN_RADIXand- Character.MAX_RADIX.
- Throws:
- java.lang.NumberFormatException- if the string does not contain a valid unsigned- int, or if supplied radix is invalid.
- java.lang.NullPointerException- if- sis null (in contrast to- Integer.parseInt(String))
 
 - 
toStringpublic static java.lang.String toString(int x) Returns a string representation of x, where x is treated as unsigned.Java 8 users: use Integer.toUnsignedString(int)instead.
 - 
toStringpublic static java.lang.String toString(int x, int radix) Returns a string representation ofxfor the given radix, wherexis treated as unsigned.Java 8 users: use Integer.toUnsignedString(int, int)instead.- Parameters:
- x- the value to convert to a string.
- radix- the radix to use while working with- x
- Throws:
- java.lang.IllegalArgumentException- if- radixis not between- Character.MIN_RADIXand- Character.MAX_RADIX.
 
 
- 
 
-