Package com.google.common.primitives
Class UnsignedInteger
- java.lang.Object
-
- java.lang.Number
-
- com.google.common.primitives.UnsignedInteger
-
- All Implemented Interfaces:
java.io.Serializable
,java.lang.Comparable<UnsignedInteger>
@GwtCompatible(emulated=true) public final class UnsignedInteger extends java.lang.Number implements java.lang.Comparable<UnsignedInteger>
A wrapper class for unsignedint
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 fromUnsignedInts
.See the Guava User Guide article on unsigned primitive utilities.
- Since:
- 11.0
- See Also:
- Serialized Form
-
-
Field Summary
Fields Modifier and Type Field Description static UnsignedInteger
MAX_VALUE
static UnsignedInteger
ONE
static UnsignedInteger
ZERO
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description java.math.BigInteger
bigIntegerValue()
Returns the value of thisUnsignedInteger
as aBigInteger
.int
compareTo(UnsignedInteger other)
Compares this unsigned integer to another unsigned integer.UnsignedInteger
dividedBy(UnsignedInteger val)
Returns the result of dividing this byval
.double
doubleValue()
Returns the value of thisUnsignedInteger
as afloat
, analogous to a widening primitive conversion fromint
todouble
, and correctly rounded.boolean
equals(java.lang.Object obj)
float
floatValue()
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
longValue()
Returns the value of thisUnsignedInteger
as along
.UnsignedInteger
minus(UnsignedInteger val)
Returns the result of subtracting this andval
.UnsignedInteger
mod(UnsignedInteger val)
Returns this modval
.UnsignedInteger
plus(UnsignedInteger val)
Returns the result of adding this andval
.UnsignedInteger
times(UnsignedInteger val)
Returns the result of multiplying this andval
.java.lang.String
toString()
Returns a string representation of theUnsignedInteger
value, in base 10.java.lang.String
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
valueOf(java.lang.String string)
Returns anUnsignedInteger
holding the value of the specifiedString
, parsed as an unsignedint
value.static UnsignedInteger
valueOf(java.lang.String string, int radix)
Returns anUnsignedInteger
holding the value of the specifiedString
, parsed as an unsignedint
value in the specified radix.static UnsignedInteger
valueOf(java.math.BigInteger value)
Returns aUnsignedInteger
representing the same value as the specifiedBigInteger
.
-
-
-
Field Detail
-
ZERO
public static final UnsignedInteger ZERO
-
ONE
public static final UnsignedInteger ONE
-
MAX_VALUE
public static final UnsignedInteger MAX_VALUE
-
-
Method Detail
-
fromIntBits
public static UnsignedInteger fromIntBits(int bits)
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
public static UnsignedInteger valueOf(long value)
Returns anUnsignedInteger
that is equal tovalue
, if possible. The inverse operation oflongValue()
.
-
valueOf
public static UnsignedInteger valueOf(java.math.BigInteger value)
Returns aUnsignedInteger
representing the same value as the specifiedBigInteger
. This is the inverse operation ofbigIntegerValue()
.- Throws:
java.lang.IllegalArgumentException
- ifvalue
is negative orvalue >= 2^32
-
valueOf
public static UnsignedInteger valueOf(java.lang.String string)
Returns anUnsignedInteger
holding the value of the specifiedString
, parsed as an unsignedint
value.- Throws:
java.lang.NumberFormatException
- if the string does not contain a parsable unsignedint
value
-
valueOf
public static UnsignedInteger valueOf(java.lang.String string, int radix)
Returns anUnsignedInteger
holding the value of the specifiedString
, parsed as an unsignedint
value in the specified radix.- Throws:
java.lang.NumberFormatException
- if the string does not contain a parsable unsignedint
value
-
plus
@CheckReturnValue public UnsignedInteger plus(UnsignedInteger val)
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
@CheckReturnValue public UnsignedInteger minus(UnsignedInteger val)
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
@CheckReturnValue @GwtIncompatible("Does not truncate correctly") public UnsignedInteger times(UnsignedInteger val)
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
@CheckReturnValue public UnsignedInteger dividedBy(UnsignedInteger val)
Returns the result of dividing this byval
.- Throws:
java.lang.ArithmeticException
- ifval
is zero- Since:
- 14.0
-
mod
@CheckReturnValue public UnsignedInteger mod(UnsignedInteger val)
Returns this modval
.- Throws:
java.lang.ArithmeticException
- ifval
is zero- Since:
- 14.0
-
intValue
public int 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
.- Specified by:
intValue
in classjava.lang.Number
-
longValue
public long longValue()
Returns the value of thisUnsignedInteger
as along
.- Specified by:
longValue
in classjava.lang.Number
-
floatValue
public float floatValue()
Returns the value of thisUnsignedInteger
as afloat
, analogous to a widening primitive conversion fromint
tofloat
, and correctly rounded.- Specified by:
floatValue
in classjava.lang.Number
-
doubleValue
public double doubleValue()
Returns the value of thisUnsignedInteger
as afloat
, analogous to a widening primitive conversion fromint
todouble
, and correctly rounded.- Specified by:
doubleValue
in classjava.lang.Number
-
bigIntegerValue
public java.math.BigInteger bigIntegerValue()
Returns the value of thisUnsignedInteger
as aBigInteger
.
-
compareTo
public int compareTo(UnsignedInteger other)
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 interfacejava.lang.Comparable<UnsignedInteger>
-
hashCode
public int hashCode()
- Overrides:
hashCode
in classjava.lang.Object
-
equals
public boolean equals(@Nullable java.lang.Object obj)
- Overrides:
equals
in classjava.lang.Object
-
toString
public java.lang.String toString()
Returns a string representation of theUnsignedInteger
value, in base 10.- Overrides:
toString
in classjava.lang.Object
-
toString
public java.lang.String toString(int radix)
Returns a string representation of theUnsignedInteger
value, in baseradix
. Ifradix < Character.MIN_RADIX
orradix > Character.MAX_RADIX
, the radix10
is used.
-
-