public final class MutableInt128 extends java.lang.Object implements java.lang.Cloneable, java.lang.Comparable<MutableInt128>
Integer signed value with 128-bit precision: −2128<x<2128.
Note: this class cannot represent positive or negative values ±2128. (In comparison, long 64-bit primitive type can represent −263.)
Unlike BigInteger, this class is mutable, that allows to reuse one instance for 128-bit calculation without memory allocation. This class is not thread-safe, but is thread-compatible and can be synchronized manually if multithread access is necessary.
Constructor and Description |
---|
MutableInt128()
Creates new instance of this class, containing integer value 0.
|
Modifier and Type | Method and Description |
---|---|
MutableInt128 |
abs()
Replaces this number with its absolute value.
|
MutableInt128 |
add(MutableInt128 other)
Adds the given other number to this one and stores the sum in this object.
|
MutableInt128 |
addLong(long value)
Adds the given integer value to this number.
|
MutableInt128 |
addLongIntProduct(long firstMultiplier,
int secondMultiplier)
Calculate exact product firstMultiplier*secondMultiplier and adds it to this number.
|
MutableInt128 |
addLongLongProduct(long firstMultiplier,
long secondMultiplier)
Calculate exact product firstMultiplier*secondMultiplier and adds it to this number.
|
MutableInt128 |
addLongSqr(long value)
Calculate exact exact square of the given value: value*value
and adds it to this number.
|
MutableInt128 |
addUnsignedLongLongProduct(long firstMultiplier,
long secondMultiplier)
Calculate exact unsigned product firstMultiplier*secondMultiplier,
where the arguments are interpreted as unsigned (non-negative) 64-bit integers,
and adds it to this number.
|
MutableInt128 |
and(MutableInt128 other)
Replaces all bits of the absolute value of this number with bitwise AND of this and other number:
|this| = |this| & |other|.
|
int |
bitCount()
Returns the number of one-bits in the 128-bit absolute value of this number.
|
MutableInt128 |
clone()
Returns an exact copy of this integer number.
|
int |
compareTo(MutableInt128 o)
Compares this number with the specified one.
|
boolean |
equals(java.lang.Object o)
Indicates whether some other object is an instance of this class, equal to this number.
|
boolean |
getBit(int index)
Returns the bit #index of the absolute value of this number.
|
int |
hashCode()
Returns the hash code of this number.
|
long |
high64Bits()
Returns high 64 bits of the absolute value of this number.
|
boolean |
isConvertibleToLong()
Returns true if and only if this number can be represented by long primitive type,
i.e.
|
boolean |
isExactlyConvertibleToDouble()
Returns true if and only if this number can be exactly represented by double
primitive type, i.e.
|
boolean |
isNegative()
Returns true if and only if this number is negative.
|
boolean |
isPositive()
Returns true if and only if this number is positive.
|
boolean |
isZero()
Returns true if and only if the value of this number is zero.
|
long |
low64Bits()
Returns low 64 bits of the absolute value of this number.
|
MutableInt128 |
maxValue()
Sets this number to maximal possible value (+2128).
|
MutableInt128 |
minValue()
Sets this number to minimal possible value (−2128).
|
MutableInt128 |
negate()
Changes the sign of this integer number and stores the result in this object.
|
static MutableInt128 |
newMaxValue()
Creates new instance of this class, containing the maxnimal possible value (+2128).
|
static MutableInt128 |
newMinValue()
Creates new instance of this class, containing the minimal possible value (−2128).
|
static MutableInt128 |
newOne()
Creates new instance of this class, containing +1.
|
static MutableInt128 |
newZero()
Creates new instance of this class, containing 0.
|
MutableInt128 |
not()
Inverts all bits of the absolute value of this number:
|this| = ~|this|.
|
int |
numberOfLeadingZeros()
Returns the number of zero bits preceding the highest-order
("leftmost") one-bit in the 128-bit absolute value of this number.
|
int |
numberOfTrailingZeros()
Returns the number of zero bits following the lowest-order ("rightmost")
one-bit in the 128-bit absolute value of this number.
|
MutableInt128 |
one()
Sets this number to one value (+1).
|
MutableInt128 |
or(MutableInt128 other)
Replaces all bits of the absolute value of this number with bitwise OR of this and other number:
|this| = |this| | |other|.
|
MutableInt128 |
setBit(int index,
boolean value)
Sets the bit #index of the absolute value of this number to the given value.
|
MutableInt128 |
setTo(MutableInt128 other)
Sets this number to be identical to the passed number.
|
MutableInt128 |
setToDouble(double value)
Sets this number to be truncated 128-bit long approximation of the passed double value.
|
MutableInt128 |
setToLong(long value)
Sets this number to be equal to the passed long value.
|
MutableInt128 |
setToLongIntProduct(long firstMultiplier,
int secondMultiplier)
Sets this object to be equal to the exact product firstMultiplier*secondMultiplier.
|
MutableInt128 |
setToLongLongProduct(long firstMultiplier,
long secondMultiplier)
Sets this object to be equal to the exact product firstMultiplier*secondMultiplier.
|
MutableInt128 |
setToLongSqr(long value)
Sets this object to be equal to the exact square of the given value: value*value.
|
MutableInt128 |
setToUnsignedLong(long value)
Sets this number to be equal to the passed long value, interpreted as unsigned 64-bit value.
|
MutableInt128 |
setToUnsignedLongLongProduct(long firstMultiplier,
long secondMultiplier)
Sets this object to be equal to the exact unsigned product firstMultiplier*secondMultiplier,
where the arguments are interpreted as unsigned 64-bit integers.
|
MutableInt128 |
shiftLeft(int shift)
Shifts all bits of the absolute value of this number leftwards by the specified number of bits.
|
MutableInt128 |
shiftRight(int shift)
Shifts all bits of the absolute value of this number rightwards by the specified number of bits.
|
MutableInt128 |
shiftRightRounding(int shift)
Divides the absolute value of this number by 2shift and rounds the result to the closest
128-bit integer value.
|
static long |
shiftRightRounding(long value,
int shift)
Divides the given value by 2shift and rounds the result to the closest
long value.
|
int |
signum()
Returns the signum function of this number.
|
MutableInt128 |
subtract(MutableInt128 other)
Subtracts the given other number from this one and stores the difference in this object.
|
java.math.BigInteger |
toBigInteger()
Converts this number into BigInteger.
|
double |
toDouble()
Converts this number into double value.
|
long |
toLongExact()
Returns this number as long value, if it is possible (this number is in range
−263≤x≤263−1).
|
java.lang.String |
toString()
Returns the decimal String representation of this number.
|
java.lang.String |
toString(int radix)
Returns the String representation of this number with the given radix.
|
static long |
unsignedShiftRightRounding(long value,
int shift)
Divides the given value, interpreted as unsigned 64-bit value, by 2shift
and rounds the result to the closest unsigned 64-bit long value.
|
static double |
unsignedToDouble(long value)
Converts the passed value, interpreted as unsigned 64-bit value, into double, and returns it.
|
static MutableInt128 |
valueOf(long value)
Creates new instance of this class, equal to the passed long value.
|
static MutableInt128 |
valueOfDouble(double value)
Creates new instance of this class, equal to the truncated 128-bit long approximation
of the passed double value.
|
static MutableInt128 |
valueOfUnsigned(long value)
Creates new instance of this class, equal to the passed long value, interpreted
as unsigned 64-bit value.
|
MutableInt128 |
xor(MutableInt128 other)
Replaces all bits of the absolute value of this number with bitwise XOR of this and other number:
|this| = |this| ^ |other|.
|
MutableInt128 |
zero()
Sets this number to zero value (0).
|
public MutableInt128()
public static MutableInt128 valueOf(long value)
This method is equivalent to new MutableInt128().setToLong(value).
value
- some long value.public static MutableInt128 valueOfUnsigned(long value)
This method is equivalent to new MutableInt128().setToUnsignedLong(value).
value
- some long value.public static MutableInt128 valueOfDouble(double value)
This method is equivalent to new MutableInt128().setToDouble(value).
value
- some long value.public static MutableInt128 newZero()
Note: this is just another form of calling the constructor
.
public static MutableInt128 newOne()
public static MutableInt128 newMinValue()
public static MutableInt128 newMaxValue()
public boolean isZero()
zero()
public boolean isPositive()
zero number
is not positive: this method returns false for it.public boolean isNegative()
zero number
is non-negative: this method returns false for it.public int signum()
public long low64Bits()
shiftRight(int)
to retrieve any bits of the absolute value.public long high64Bits()
The result of this method is equal to the following expression:
thisNumber.shiftRight
(64).low64Bits()
but this method does not change the state of this object.
public boolean isExactlyConvertibleToDouble()
In other words, this method returns true if and only if the method toDouble()
is performed absolutely exactly, without precision loss.
public double toDouble()
Note: the returned value is en exact representation of this number,
if the method isExactlyConvertibleToDouble()
returns true.
In this case, the reverse
public boolean isConvertibleToLong()
In other words, returns true if and only if the method toLongExact()
does not throw
an exception.
public long toLongExact()
You can check, whether this number can be represented as long, with help of
isConvertibleToLong()
method.
java.lang.ArithmeticException
- in a case of arithmetic overflow.public java.math.BigInteger toBigInteger()
public MutableInt128 setTo(MutableInt128 other)
other
- other number.java.lang.NullPointerException
- if the argument is null.public MutableInt128 setToLong(long value)
value
- some long value.public MutableInt128 setToUnsignedLong(long value)
value
- some long value (interpreted as unsigned 64-bit integer).public MutableInt128 setToDouble(double value)
value
- some double value.public MutableInt128 zero()
public MutableInt128 one()
public MutableInt128 minValue()
public MutableInt128 maxValue()
public boolean getBit(int index)
index
- index of the bit of this number.java.lang.IllegalArgumentException
- if index argument is negative.public MutableInt128 setBit(int index, boolean value)
index
- index of the bit of this number.value
- new value of the bit (true is 1, false is 0).java.lang.IllegalArgumentException
- if index argument is negative.public MutableInt128 shiftRight(int shift)
x<0 ? −(−x)/2shift : x/2shift.
If the argument shift is too large (≥128), the method sets this number to zero.
Shifting by 0 bits (shift=0) or shifting zero value with any shift does not change the number.
shift
- number of bits to shift.java.lang.IllegalArgumentException
- if shift argument is negative.public MutableInt128 shiftLeft(int shift)
x<0 ? −(−x)*2shift : x*2shift.
If the resulting absolute value cannot be represented by 128-bit number, in particular, if current number is non-zero and shift≥128, the method throws ArithmeticException.
Shifting by 0 bits (shift=0) or shifting zero value with any shift does not change the number.
shift
- number of bits to shift.java.lang.IllegalArgumentException
- if shift argument is negative.java.lang.ArithmeticException
- in a case of arithmetic overflow.public MutableInt128 shiftRightRounding(int shift)
[x/2.0shift],where [w] means the mathematical integer, closest to w, like in Math.rint() method.
If the argument shift is too large (≥128), the method sets this number to zero.
Shifting by 0 bits (shift=0) or shifting zero value with any shift does not change the number.
shift
- number of bits to shift.java.lang.IllegalArgumentException
- if shift argument is negative.public MutableInt128 and(MutableInt128 other)
The sign of this number stays unchanged — excepting the case, when this number was non-zero, but the result is zero (i.e. not positive and not negative).
other
- value to be AND'ed with this number.public MutableInt128 or(MutableInt128 other)
The sign of this number stays unchanged — excepting the case, when this number was zero, but the result is nonzero (i.e. positive or negative).
other
- value to be OR'ed with this number.public MutableInt128 xor(MutableInt128 other)
The sign of this number stays unchanged — excepting the case, when this number or the result is zero (i.e. not positive and not negative).
other
- value to be XOR'ed with this number.public MutableInt128 not()
The sign of this number stays unchanged — excepting the case, when this number or the result is zero (i.e. not positive and not negative).
public int numberOfLeadingZeros()
It is a 128-bit analogue of the standard Long.numberOfLeadingZeros method, but working with the absolute value of this number (instead of two's complement binary representation).
public int numberOfTrailingZeros()
It is a 128-bit analogue of the standard Long.numberOfTrailingZeros method, but working with the absolute value of this number (instead of two's complement binary representation).
public int bitCount()
It is a 128-bit analogue of the standard Long.bitCount method, but working with the absolute value of this number (instead of two's complement binary representation).
public MutableInt128 negate()
zero number
.public MutableInt128 abs()
public MutableInt128 add(MutableInt128 other) throws java.lang.ArithmeticException
java.lang.ArithmeticException
- in a case of arithmetic overflow.java.lang.NullPointerException
- if the argument is null.public MutableInt128 subtract(MutableInt128 other) throws java.lang.ArithmeticException
java.lang.ArithmeticException
- in a case of arithmetic overflow.java.lang.NullPointerException
- if the argument is null.public MutableInt128 addLong(long value)
value
- any long value to add.java.lang.ArithmeticException
- in a case of arithmetic overflow.public MutableInt128 setToLongLongProduct(long firstMultiplier, long secondMultiplier)
firstMultiplier
- first multiplicand.secondMultiplier
- second multiplicand.public MutableInt128 setToLongIntProduct(long firstMultiplier, int secondMultiplier)
Note: this method works faster than setToLongLongProduct(long, long)
.
firstMultiplier
- first multiplicand.secondMultiplier
- second multiplicand.public MutableInt128 setToUnsignedLongLongProduct(long firstMultiplier, long secondMultiplier)
Note: this method works little faster than setToLongLongProduct(long, long)
.
firstMultiplier
- first multiplicand (interpreted as unsigned 64-bit integer number).secondMultiplier
- second multiplicand (interpreted as unsigned 64-bit integer number).public MutableInt128 setToLongSqr(long value)
setToLongLongProduct(value, value)
value
- some long signed value.public MutableInt128 addLongLongProduct(long firstMultiplier, long secondMultiplier)
add
(newMutableLongLong
().setToLongLongProduct
(firstMultiplier, secondMultiplier))
firstMultiplier
- first multiplicand.secondMultiplier
- second multiplicand.java.lang.ArithmeticException
- in a case of arithmetic overflow while adding.public MutableInt128 addLongIntProduct(long firstMultiplier, int secondMultiplier)
add
(newMutableLongLong
().setToLongIntProduct
(firstMultiplier, secondMultiplier))
Note: this method works faster than addLongLongProduct(long, long)
.
firstMultiplier
- first multiplicand.secondMultiplier
- second multiplicand.java.lang.ArithmeticException
- in a case of arithmetic overflow while adding.public MutableInt128 addUnsignedLongLongProduct(long firstMultiplier, long secondMultiplier)
add
(newMutableLongLong
().setToUnsignedLongLongProduct
(firstMultiplier, secondMultiplier))
Note: this method works little faster than addLongLongProduct(long, long)
.
firstMultiplier
- first multiplicand (interpreted as unsigned 64-bit integer number).secondMultiplier
- second multiplicand (interpreted as unsigned 64-bit integer number).java.lang.ArithmeticException
- in a case of arithmetic overflow while adding.public MutableInt128 addLongSqr(long value)
add
(newMutableLongLong
().setToLongSqr
(value))
value
- some long signed value.java.lang.ArithmeticException
- in a case of arithmetic overflow while adding.public MutableInt128 clone()
clone
in class java.lang.Object
public int compareTo(MutableInt128 o)
compareTo
in interface java.lang.Comparable<MutableInt128>
o
- other number to which this number is to be compared.o
.java.lang.NullPointerException
- if the argument is null.public java.lang.String toString()
toString
in class java.lang.Object
public java.lang.String toString(int radix)
Character.MIN_RADIX
to Character.MAX_RADIX
inclusive,
it will default to 10 (as is the case for
Integer.toString
).radix
- radix of the String representation.public boolean equals(java.lang.Object o)
zero number
,
are always equal, regardless on possible calling negate()
method.equals
in class java.lang.Object
o
- the object to be compared for equality with this instance.MutableInt128
,
containing the same mathematical integer number as this object.public int hashCode()
hashCode
in class java.lang.Object
public static double unsignedToDouble(long value)
value
- some long value (interpreted as unsigned 64-bit integer).public static long shiftRightRounding(long value, int shift)
[value/2.0shift],where [w] means the mathematical integer, closest to w, like in Math.rint() method.
If the argument shift is too large (≥64), the method returns 0.
Shifting by 0 bits (shift=0) returns unchanged value.
value
- some (signed) long value.shift
- number of bits to shift.java.lang.IllegalArgumentException
- if shift argument is negative.public static long unsignedShiftRightRounding(long value, int shift)
shiftRightRounding(long, int)
method.
If the argument shift is too large (≥64), the method returns 0.
Shifting by 0 bits (shift=0) returns unchanged value.
value
- some (unsigned) 64-bit integer value.shift
- number of bits to shift.java.lang.IllegalArgumentException
- if shift argument is negative.