Class UnsignedInts
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.
- Since:
- 11.0
- Author:
- Louis Wasserman
-
Method Summary
Modifier and TypeMethodDescriptionstatic int
checkedCast
(long value) Returns theint
value that, when treated as unsigned, is equal tovalue
, if possible.static int
compare
(int a, int b) Compares the two specifiedint
values, treating them as unsigned values between0
and2^32 - 1
inclusive.static int
Returns the unsignedint
value represented by the given string.static int
divide
(int dividend, int divisor) Returns dividend / divisor, where the dividend and divisor are treated as unsigned 32-bit quantities.static String
Returns a string containing the supplied unsignedint
values separated byseparator
.static Comparator
<int[]> Returns a comparator that compares two arrays of unsignedint
values lexicographically.static int
max
(int... array) Returns the greatest value present inarray
, treating values as unsigned.static int
min
(int... array) Returns the least value present inarray
, treating values as unsigned.static int
Returns the unsignedint
value represented by the given decimal string.static int
parseUnsignedInt
(String string, int radix) Returns the unsignedint
value represented by a string with the given radix.static int
remainder
(int dividend, int divisor) Returns dividend % divisor, where the dividend and divisor are treated as unsigned 32-bit quantities.static int
saturatedCast
(long value) Returns theint
value that, when treated as unsigned, is nearest in value tovalue
.static void
sort
(int[] array) Sorts the array, treating its elements as unsigned 32-bit integers.static void
sort
(int[] array, int fromIndex, int toIndex) Sorts the array betweenfromIndex
inclusive andtoIndex
exclusive, treating its elements as unsigned 32-bit integers.static void
sortDescending
(int[] array) Sorts the elements ofarray
in descending order, interpreting them as unsigned 32-bit integers.static void
sortDescending
(int[] array, int fromIndex, int toIndex) Sorts the elements ofarray
betweenfromIndex
inclusive andtoIndex
exclusive in descending order, interpreting them as unsigned 32-bit integers.static long
toLong
(int value) Returns the value of the givenint
as along
, when treated as unsigned.static String
toString
(int x) Returns a string representation of x, where x is treated as unsigned.static String
toString
(int x, int radix) Returns a string representation ofx
for the given radix, wherex
is treated as unsigned.
-
Method Details
-
compare
Compares the two specifiedint
values, treating them as unsigned values between0
and2^32 - 1
inclusive.Note: this method is now unnecessary and should be treated as deprecated; use the equivalent
Integer.compareUnsigned(int, int)
method instead.- Parameters:
a
- the first unsignedint
to compareb
- the second unsignedint
to compare- Returns:
- a negative value if
a
is less thanb
; a positive value ifa
is greater thanb
; or zero if they are equal
-
toLong
Returns the value of the givenint
as along
, when treated as unsigned.Java 8+ users: use
Integer.toUnsignedLong(int)
instead. -
checkedCast
Returns theint
value that, when treated as unsigned, is equal tovalue
, if possible.- Parameters:
value
- a value between 0 and 232-1 inclusive- Returns:
- the
int
value that, when treated as unsigned, equalsvalue
- Throws:
IllegalArgumentException
- ifvalue
is negative or greater than or equal to 232- Since:
- 21.0
-
saturatedCast
Returns theint
value that, when treated as unsigned, is nearest in value tovalue
.- Parameters:
value
- anylong
value- Returns:
2^32 - 1
ifvalue >= 2^32
,0
ifvalue <= 0
, andvalue
cast toint
otherwise- Since:
- 21.0
-
min
Returns the least value present inarray
, treating values as unsigned.- Parameters:
array
- a nonempty array of unsignedint
values- Returns:
- the value present in
array
that is less than or equal to every other value in the array according tocompare(int, int)
- Throws:
IllegalArgumentException
- ifarray
is empty
-
max
Returns the greatest value present inarray
, treating values as unsigned.- Parameters:
array
- a nonempty array of unsignedint
values- Returns:
- the value present in
array
that is greater than or equal to every other value in the array according tocompare(int, int)
- Throws:
IllegalArgumentException
- ifarray
is empty
-
join
Returns a string containing the supplied unsignedint
values 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 unsignedint
values, possibly empty
-
lexicographicalComparator
Returns a comparator that compares two arrays of unsignedint
values 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[])
. -
sort
Sorts the array, treating its elements as unsigned 32-bit integers.- Since:
- 23.1
-
sort
Sorts the array betweenfromIndex
inclusive andtoIndex
exclusive, treating its elements as unsigned 32-bit integers.- Since:
- 23.1
-
sortDescending
Sorts the elements ofarray
in descending order, interpreting them as unsigned 32-bit integers.- Since:
- 23.1
-
sortDescending
Sorts the elements ofarray
betweenfromIndex
inclusive andtoIndex
exclusive in descending order, interpreting them as unsigned 32-bit integers.- Since:
- 23.1
-
divide
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:
ArithmeticException
- if divisor is 0
-
remainder
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:
ArithmeticException
- if divisor is 0
-
decode
Returns the unsignedint
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:
NumberFormatException
- if the string does not contain a valid unsignedint
value- Since:
- 13.0
-
parseUnsignedInt
Returns the unsignedint
value represented by the given decimal string.Java 8+ users: use
Integer.parseUnsignedInt(String)
instead.- Throws:
NumberFormatException
- if the string does not contain a valid unsignedint
valueNullPointerException
- ifs
is null (in contrast toInteger.parseInt(String)
)
-
parseUnsignedInt
Returns the unsignedint
value 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 parsings
; must be betweenCharacter.MIN_RADIX
andCharacter.MAX_RADIX
.- Throws:
NumberFormatException
- if the string does not contain a valid unsignedint
, or if supplied radix is invalid.NullPointerException
- ifs
is null (in contrast toInteger.parseInt(String)
)
-
toString
Returns a string representation of x, where x is treated as unsigned.Java 8+ users: use
Integer.toUnsignedString(int)
instead. -
toString
Returns a string representation ofx
for the given radix, wherex
is 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 withx
- Throws:
IllegalArgumentException
- ifradix
is not betweenCharacter.MIN_RADIX
andCharacter.MAX_RADIX
.
-