Class UnsignedInteger
java.lang.Object
java.lang.Number
com.google.common.primitives.UnsignedInteger
- All Implemented Interfaces:
Serializable
,Comparable<UnsignedInteger>
@GwtCompatible(emulated=true)
public final class UnsignedInteger
extends Number
implements Comparable<UnsignedInteger>
A wrapper class for unsigned
int
values, supporting arithmetic operations.
In some cases, when speed is more important than code readability, it may be faster simply to
treat primitive int
values as unsigned, using the methods from UnsignedInts
.
See the Guava User Guide article on unsigned primitive utilities.
- Since:
- 11.0
- Author:
- Louis Wasserman
- See Also:
-
Field Summary
Modifier and TypeFieldDescriptionstatic final UnsignedInteger
static final UnsignedInteger
static final UnsignedInteger
-
Method Summary
Modifier and TypeMethodDescriptionReturns the value of thisUnsignedInteger
as aBigInteger
.int
compareTo
(UnsignedInteger other) Compares this unsigned integer to another unsigned integer.dividedBy
(UnsignedInteger val) Returns the result of dividing this byval
.double
Returns the value of thisUnsignedInteger
as adouble
, analogous to a widening primitive conversion fromint
todouble
, and correctly rounded.boolean
float
Returns the value of thisUnsignedInteger
as afloat
, analogous to a widening primitive conversion fromint
tofloat
, and correctly rounded.static UnsignedInteger
fromIntBits
(int bits) Returns anUnsignedInteger
corresponding to a given bit representation.int
hashCode()
int
intValue()
Returns the value of thisUnsignedInteger
as anint
.long
Returns the value of thisUnsignedInteger
as along
.minus
(UnsignedInteger val) Returns the result of subtracting this andval
.mod
(UnsignedInteger val) Returns this modval
.plus
(UnsignedInteger val) Returns the result of adding this andval
.times
(UnsignedInteger val) Returns the result of multiplying this andval
.toString()
Returns a string representation of theUnsignedInteger
value, in base 10.toString
(int radix) Returns a string representation of theUnsignedInteger
value, in baseradix
.static UnsignedInteger
valueOf
(long value) Returns anUnsignedInteger
that is equal tovalue
, if possible.static UnsignedInteger
Returns anUnsignedInteger
holding the value of the specifiedString
, parsed as an unsignedint
value.static UnsignedInteger
Returns anUnsignedInteger
holding the value of the specifiedString
, parsed as an unsignedint
value in the specified radix.static UnsignedInteger
valueOf
(BigInteger value) Returns aUnsignedInteger
representing the same value as the specifiedBigInteger
.Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
ZERO
-
ONE
-
MAX_VALUE
-
-
Method Details
-
fromIntBits
Returns anUnsignedInteger
corresponding to a given bit representation. The argument is interpreted as an unsigned 32-bit value. Specifically, the sign bit ofbits
is interpreted as a normal bit, and all other bits are treated as usual.If the argument is nonnegative, the returned result will be equal to
bits
, otherwise, the result will be equal to2^32 + bits
.To represent unsigned decimal constants, consider
valueOf(long)
instead.- Since:
- 14.0
-
valueOf
Returns anUnsignedInteger
that is equal tovalue
, if possible. The inverse operation oflongValue()
. -
valueOf
Returns aUnsignedInteger
representing the same value as the specifiedBigInteger
. This is the inverse operation ofbigIntegerValue()
.- Throws:
IllegalArgumentException
- ifvalue
is negative orvalue >= 2^32
-
valueOf
Returns anUnsignedInteger
holding the value of the specifiedString
, parsed as an unsignedint
value.- Throws:
NumberFormatException
- if the string does not contain a parsable unsignedint
value
-
valueOf
Returns anUnsignedInteger
holding the value of the specifiedString
, parsed as an unsignedint
value in the specified radix.- Throws:
NumberFormatException
- if the string does not contain a parsable unsignedint
value
-
plus
Returns the result of adding this andval
. If the result would have more than 32 bits, returns the low 32 bits of the result.- Since:
- 14.0
-
minus
Returns the result of subtracting this andval
. If the result would be negative, returns the low 32 bits of the result.- Since:
- 14.0
-
times
Returns the result of multiplying this andval
. If the result would have more than 32 bits, returns the low 32 bits of the result.- Since:
- 14.0
-
dividedBy
Returns the result of dividing this byval
.- Throws:
ArithmeticException
- ifval
is zero- Since:
- 14.0
-
mod
Returns this modval
.- Throws:
ArithmeticException
- ifval
is zero- Since:
- 14.0
-
intValue
Returns the value of thisUnsignedInteger
as anint
. This is an inverse operation tofromIntBits(int)
.Note that if this
UnsignedInteger
holds a value>= 2^31
, the returned value will be equal tothis - 2^32
. -
longValue
-
floatValue
Returns the value of thisUnsignedInteger
as afloat
, analogous to a widening primitive conversion fromint
tofloat
, and correctly rounded.- Specified by:
floatValue
in classNumber
-
doubleValue
Returns the value of thisUnsignedInteger
as adouble
, analogous to a widening primitive conversion fromint
todouble
, and correctly rounded.- Specified by:
doubleValue
in classNumber
-
bigIntegerValue
Returns the value of thisUnsignedInteger
as aBigInteger
. -
compareTo
Compares this unsigned integer to another unsigned integer. Returns0
if they are equal, a negative number ifthis < other
, and a positive number ifthis > other
.- Specified by:
compareTo
in interfaceComparable<UnsignedInteger>
-
hashCode
-
equals
-
toString
-
toString
-