@Beta @GwtCompatible public final class UnsignedInts extends Object
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.
Modifier and Type  Method and Description 

static int 
checkedCast(long value)
Returns the
int value that, when treated as unsigned, is equal to value , if
possible. 
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. 
static int 
decode(String stringValue)
Returns the unsigned
int 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 32bit
quantities.

static String 
join(String separator,
int... array)
Returns a string containing the supplied unsigned
int values separated by
separator . 
static Comparator<int[]> 
lexicographicalComparator()
Returns a comparator that compares two arrays of unsigned
int values lexicographically. 
static int 
max(int... array)
Returns the greatest value present in
array , treating values as unsigned. 
static int 
min(int... array)
Returns the least value present in
array , treating values as unsigned. 
static int 
parseUnsignedInt(String s)
Returns the unsigned
int value represented by the given decimal string. 
static int 
parseUnsignedInt(String string,
int radix)
Returns the unsigned
int 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 32bit
quantities.

static int 
saturatedCast(long value)
Returns the
int value that, when treated as unsigned, is nearest in value to
value . 
static long 
toLong(int value)
Returns the value of the given
int as a long , 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 of
x for the given radix, where x is treated as
unsigned. 
public static int compare(int a, int b)
int
values, treating them as unsigned values between
0
and 2^32  1
inclusive.a
 the first unsigned int
to compareb
 the second unsigned int
to comparea
is less than b
; a positive value if a
is
greater than b
; or zero if they are equalpublic static long toLong(int value)
int
as a long
, when treated as unsigned.public static int checkedCast(long value)
int
value that, when treated as unsigned, is equal to value
, if
possible.value
 a value between 0 and 2^{32}1 inclusiveint
value that, when treated as unsigned, equals value
IllegalArgumentException
 if value
is negative or greater than or equal to
2^{32}public static int saturatedCast(long value)
int
value that, when treated as unsigned, is nearest in value to
value
.value
 any long
value2^32  1
if value >= 2^32
, 0
if value <= 0
, and
value
cast to int
otherwisepublic static int min(int... array)
array
, treating values as unsigned.array
 a nonempty array of unsigned int
valuesarray
that is less than or equal to every other value in
the array according to compare(int, int)
IllegalArgumentException
 if array
is emptypublic static int max(int... array)
array
, treating values as unsigned.array
 a nonempty array of unsigned int
valuesarray
that is greater than or equal to every other value
in the array according to compare(int, int)
IllegalArgumentException
 if array
is emptypublic static String join(String separator, int... array)
int
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 int
values, possibly emptypublic static Comparator<int[]> lexicographicalComparator()
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[])
.
public static int divide(int dividend, int divisor)
dividend
 the dividend (numerator)divisor
 the divisor (denominator)ArithmeticException
 if divisor is 0public static int remainder(int dividend, int divisor)
dividend
 the dividend (numerator)divisor
 the divisor (denominator)ArithmeticException
 if divisor is 0public static int decode(String stringValue)
int
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 int
valuepublic static int parseUnsignedInt(String s)
int
value represented by the given decimal string.NumberFormatException
 if the string does not contain a valid unsigned int
valueNullPointerException
 if s
is null (in contrast to
Integer.parseInt(String)
)public static int parseUnsignedInt(String string, int radix)
int
value represented by a string with the given radix.string
 the string containing the unsigned integer representation to be parsed.radix
 the radix to use while parsing s
; must be between
Character.MIN_RADIX
and Character.MAX_RADIX
.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)
)public static String toString(int x)
public static String toString(int x, int radix)
x
for the given radix, where x
is treated as
unsigned.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–2017. All rights reserved.