Class TBigInteger
- All Implemented Interfaces:
Serializable
,Comparable<TBigInteger>
Since the class was modeled to offer all the functionality as the
Integer
class does, it provides even methods that operate bitwise on
a two's complement representation of large integers. Note however that the
implementations favors an internal representation where magnitude and sign
are treated separately. Hence such operations are inefficient and should be
discouraged. In simple words: Do NOT implement any bit fields based on
BigInteger.
- See Also:
- Serialized Form
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic TBigInteger
TheBigInteger
constant 1.static TBigInteger
TheBigInteger
constant 10.static TBigInteger
TheBigInteger
constant 0. -
Constructor Summary
ConstructorsConstructorDescriptionTBigInteger(byte[] val)
Constructs a newBigInteger
from the given two's complement representation.TBigInteger(int signum, byte[] magnitude)
Constructs a newBigInteger
instance with the given sign and the given magnitude.TBigInteger(int bitLength, int certainty, Random rnd)
Constructs a randomBigInteger
instance in the range [0, 2^(bitLength)-1] which is probably prime.TBigInteger(int numBits, Random rnd)
Constructs a random non-negativeBigInteger
instance in the range [0, 2^(numBits)-1].TBigInteger(String val)
Constructs a newBigInteger
instance from the string representation.TBigInteger(String val, int radix)
Constructs a newBigInteger
instance from the string representation. -
Method Summary
Modifier and TypeMethodDescriptionabs()
Returns a (new)BigInteger
whose value is the absolute value ofthis
.add(TBigInteger val)
Returns a newBigInteger
whose value isthis + val
.and(TBigInteger val)
Returns a newBigInteger
whose value isthis & val
.andNot(TBigInteger val)
Returns a newBigInteger
whose value isthis & ~val
.int
bitCount()
UsebitLength(0)
if you want to know the length of the binary value in bits.int
Returns the length of the value's two's complement representation without leading zeros for positive numbers / without leading ones for negative values.clearBit(int n)
Returns a newBigInteger
which has the same binary representation asthis
but with the bit at position n cleared.int
compareTo(TBigInteger val)
Compares thisBigInteger
withval
.divide(TBigInteger divisor)
Returns a newBigInteger
whose value isthis / divisor
.divideAndRemainder(TBigInteger divisor)
Returns aBigInteger
array which containsthis / divisor
at index 0 andthis % divisor
at index 1.double
Returns thisBigInteger
as an double value.boolean
Returnstrue
ifx
is a BigInteger instance and if this instance is equal to thisBigInteger
.flipBit(int n)
Returns a newBigInteger
which has the same binary representation asthis
but with the bit at position n flipped.float
Returns thisBigInteger
as an float value.gcd(TBigInteger val)
Returns a newBigInteger
whose value is greatest common divisor ofthis
andval
.int
Returns the position of the lowest set bit in the two's complement representation of thisBigInteger
.int
hashCode()
Returns a hash code for thisBigInteger
.int
intValue()
Returns thisBigInteger
as an int value.boolean
isProbablePrime(int certainty)
Tests whether thisBigInteger
is probably prime.long
Returns thisBigInteger
as an long value.max(TBigInteger val)
Returns the maximum of thisBigInteger
andval
.min(TBigInteger val)
Returns the minimum of thisBigInteger
andval
.mod(TBigInteger m)
Returns a newBigInteger
whose value isthis mod m
.Returns a newBigInteger
whose value is1/this mod m
.modPow(TBigInteger exponent, TBigInteger m)
Returns a newBigInteger
whose value isthis^exponent mod m
.multiply(TBigInteger val)
Returns a newBigInteger
whose value isthis * val
.negate()
Returns a newBigInteger
whose value is the-this
.Returns the smallest integer x >this
which is probably prime as aBigInteger
instance.not()
Returns a newBigInteger
whose value is~this
.or(TBigInteger val)
Returns a newBigInteger
whose value isthis | val
.pow(int exp)
Returns a newBigInteger
whose value isthis ^ exp
.static TBigInteger
probablePrime(int bitLength, Random rnd)
Returns a random positiveBigInteger
instance in the range [0, 2^(bitLength)-1] which is probably prime.remainder(TBigInteger divisor)
Returns a newBigInteger
whose value isthis % divisor
.setBit(int n)
Returns a newBigInteger
which has the same binary representation asthis
but with the bit at position n set.shiftLeft(int n)
Returns a newBigInteger
whose value isthis << n
.shiftRight(int n)
Returns a newBigInteger
whose value isthis >> n
.int
signum()
Returns the sign of thisBigInteger
.subtract(TBigInteger val)
Returns a newBigInteger
whose value isthis - val
.boolean
testBit(int n)
Tests whether the bit at position n inthis
is set.byte[]
Returns the two's complement representation of this BigInteger in a byte array.toString()
Returns a string representation of thisBigInteger
in decimal form.toString(int radix)
Returns a string containing a string representation of thisBigInteger
with base radix.static TBigInteger
valueOf(long val)
xor(TBigInteger val)
Returns a newBigInteger
whose value isthis ^ val
.Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
ZERO
TheBigInteger
constant 0. -
ONE
TheBigInteger
constant 1. -
TEN
TheBigInteger
constant 10.
-
-
Constructor Details
-
TBigInteger
Constructs a random non-negativeBigInteger
instance in the range [0, 2^(numBits)-1].- Parameters:
numBits
- maximum length of the newBigInteger
in bits.rnd
- is an optional random generator to be used.- Throws:
IllegalArgumentException
- ifnumBits
< 0.
-
TBigInteger
Constructs a randomBigInteger
instance in the range [0, 2^(bitLength)-1] which is probably prime. The probability that the returnedBigInteger
is prime is beyond (1-1/2^certainty).- Parameters:
bitLength
- length of the newBigInteger
in bits.certainty
- tolerated primality uncertainty.rnd
- is an optional random generator to be used.- Throws:
ArithmeticException
- ifbitLength
< 2.
-
TBigInteger
Constructs a newBigInteger
instance from the string representation. The string representation consists of an optional minus sign followed by a non-empty sequence of decimal digits.- Parameters:
val
- string representation of the newBigInteger
.- Throws:
NullPointerException
- ifval == null
.NumberFormatException
- ifval
is not a valid representation of aBigInteger
.
-
TBigInteger
Constructs a newBigInteger
instance from the string representation. The string representation consists of an optional minus sign followed by a non-empty sequence of digits in the specified radix. For the conversion the methodCharacter.digit(char, radix)
is used.- Parameters:
val
- string representation of the newBigInteger
.radix
- the base to be used for the conversion.- Throws:
NullPointerException
- ifval == null
.NumberFormatException
- ifval
is not a valid representation of aBigInteger
or ifradix < Character.MIN_RADIX
orradix > Character.MAX_RADIX
.
-
TBigInteger
public TBigInteger(int signum, byte[] magnitude)Constructs a newBigInteger
instance with the given sign and the given magnitude. The sign is given as an integer (-1 for negative, 0 for zero, 1 for positive). The magnitude is specified as a byte array. The most significant byte is the entry at index 0.- Parameters:
signum
- sign of the newBigInteger
(-1 for negative, 0 for zero, 1 for positive).magnitude
- magnitude of the newBigInteger
with the most significant byte first.- Throws:
NullPointerException
- ifmagnitude == null
.NumberFormatException
- if the sign is not one of -1, 0, 1 or if the sign is zero and the magnitude contains non-zero entries.
-
TBigInteger
public TBigInteger(byte[] val)Constructs a newBigInteger
from the given two's complement representation. The most significant byte is the entry at index 0. The most significant bit of this entry determines the sign of the newBigInteger
instance. The given array must not be empty.- Parameters:
val
- two's complement representation of the newBigInteger
.- Throws:
NullPointerException
- ifval == null
.NumberFormatException
- if the length ofval
is zero.
-
-
Method Details
-
valueOf
-
toByteArray
public byte[] toByteArray()Returns the two's complement representation of this BigInteger in a byte array.- Returns:
- two's complement representation of
this
.
-
abs
Returns a (new)BigInteger
whose value is the absolute value ofthis
.- Returns:
abs(this)
.
-
negate
Returns a newBigInteger
whose value is the-this
.- Returns:
-this
.
-
add
Returns a newBigInteger
whose value isthis + val
.- Parameters:
val
- value to be added tothis
.- Returns:
this + val
.- Throws:
NullPointerException
- ifval == null
.
-
subtract
Returns a newBigInteger
whose value isthis - val
.- Parameters:
val
- value to be subtracted fromthis
.- Returns:
this - val
.- Throws:
NullPointerException
- ifval == null
.
-
signum
public int signum()Returns the sign of thisBigInteger
.- Returns:
-1
ifthis < 0
,0
ifthis == 0
,1
ifthis > 0
.
-
shiftRight
Returns a newBigInteger
whose value isthis >> n
. For negative arguments, the result is also negative. The shift distance may be negative which means thatthis
is shifted left.Implementation Note: Usage of this method on negative values is not recommended as the current implementation is not efficient.
- Parameters:
n
- shift distance- Returns:
this >> n
ifn >= 0
;this << (-n)
otherwise
-
shiftLeft
Returns a newBigInteger
whose value isthis << n
. The result is equivalent tothis * 2^n
if n >= 0. The shift distance may be negative which means thatthis
is shifted right. The result then corresponds tofloor(this / 2^(-n))
.Implementation Note: Usage of this method on negative values is not recommended as the current implementation is not efficient.
- Parameters:
n
- shift distance.- Returns:
this << n
ifn >= 0
;this >> (-n)
. otherwise
-
bitLength
public int bitLength()Returns the length of the value's two's complement representation without leading zeros for positive numbers / without leading ones for negative values.The two's complement representation of
this
will be at leastbitLength() + 1
bits long.The value will fit into an
int
ifbitLength() < 32
or into along
ifbitLength() < 64
.- Returns:
- the length of the minimal two's complement representation for
this
without the sign bit.
-
testBit
public boolean testBit(int n)Tests whether the bit at position n inthis
is set. The result is equivalent tothis & (2^n) != 0
.Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
- Parameters:
n
- position where the bit inthis
has to be inspected.- Returns:
this & (2^n) != 0
.- Throws:
ArithmeticException
- ifn < 0
.
-
setBit
Returns a newBigInteger
which has the same binary representation asthis
but with the bit at position n set. The result is equivalent tothis | 2^n
.Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
- Parameters:
n
- position where the bit inthis
has to be set.- Returns:
this | 2^n
.- Throws:
ArithmeticException
- ifn < 0
.
-
clearBit
Returns a newBigInteger
which has the same binary representation asthis
but with the bit at position n cleared. The result is equivalent tothis & ~(2^n)
.Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
- Parameters:
n
- position where the bit inthis
has to be cleared.- Returns:
this & ~(2^n)
.- Throws:
ArithmeticException
- ifn < 0
.
-
flipBit
Returns a newBigInteger
which has the same binary representation asthis
but with the bit at position n flipped. The result is equivalent tothis ^ 2^n
.Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
- Parameters:
n
- position where the bit inthis
has to be flipped.- Returns:
this ^ 2^n
.- Throws:
ArithmeticException
- ifn < 0
.
-
getLowestSetBit
public int getLowestSetBit()Returns the position of the lowest set bit in the two's complement representation of thisBigInteger
. If all bits are zero (this=0) then -1 is returned as result.Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
- Returns:
- position of lowest bit if
this != 0
,-1
otherwise
-
bitCount
public int bitCount()UsebitLength(0)
if you want to know the length of the binary value in bits.Returns the number of bits in the binary representation of
this
which differ from the sign bit. Ifthis
is positive the result is equivalent to the number of bits set in the binary representation ofthis
. Ifthis
is negative the result is equivalent to the number of bits set in the binary representation of-this-1
.Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
- Returns:
- number of bits in the binary representation of
this
which differ from the sign bit
-
not
Returns a newBigInteger
whose value is~this
. The result of this operation is-this-1
.Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
- Returns:
~this
.
-
and
Returns a newBigInteger
whose value isthis & val
.Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
- Parameters:
val
- value to be and'ed withthis
.- Returns:
this & val
.- Throws:
NullPointerException
- ifval == null
.
-
or
Returns a newBigInteger
whose value isthis | val
.Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
- Parameters:
val
- value to be or'ed withthis
.- Returns:
this | val
.- Throws:
NullPointerException
- ifval == null
.
-
xor
Returns a newBigInteger
whose value isthis ^ val
.Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
- Parameters:
val
- value to be xor'ed withthis
- Returns:
this ^ val
- Throws:
NullPointerException
- ifval == null
-
andNot
Returns a newBigInteger
whose value isthis & ~val
. Evaluatingx.andNot(val)
returns the same result asx.and(val.not())
.Implementation Note: Usage of this method is not recommended as the current implementation is not efficient.
- Parameters:
val
- value to be not'ed and then and'ed withthis
.- Returns:
this & ~val
.- Throws:
NullPointerException
- ifval == null
.
-
intValue
public int intValue()Returns thisBigInteger
as an int value. Ifthis
is too big to be represented as an int, thenthis
% 2^32 is returned. -
longValue
public long longValue()Returns thisBigInteger
as an long value. Ifthis
is too big to be represented as an long, thenthis
% 2^64 is returned. -
floatValue
public float floatValue()Returns thisBigInteger
as an float value. Ifthis
is too big to be represented as an float, thenFloat.POSITIVE_INFINITY
orFloat.NEGATIVE_INFINITY
is returned. Note, that not all integers x in the range [-Float.MAX_VALUE, Float.MAX_VALUE] can be represented as a float. The float representation has a mantissa of length 24. For example, 2^24+1 = 16777217 is returned as float 16777216.0.- Specified by:
floatValue
in classNumber
- Returns:
- this
BigInteger
as a float value.
-
doubleValue
public double doubleValue()Returns thisBigInteger
as an double value. Ifthis
is too big to be represented as an double, thenDouble.POSITIVE_INFINITY
orDouble.NEGATIVE_INFINITY
is returned. Note, that not all integers x in the range [-Double.MAX_VALUE, Double.MAX_VALUE] can be represented as a double. The double representation has a mantissa of length 53. For example, 2^53+1 = 9007199254740993 is returned as double 9007199254740992.0.- Specified by:
doubleValue
in classNumber
- Returns:
- this
BigInteger
as a double value
-
compareTo
Compares thisBigInteger
withval
. Returns one of the three values 1, 0, or -1.- Specified by:
compareTo
in interfaceComparable<TBigInteger>
- Parameters:
val
- value to be compared withthis
.- Returns:
1
ifthis > val
,-1
ifthis < val
,0
ifthis == val
.- Throws:
NullPointerException
- ifval == null
.
-
min
Returns the minimum of thisBigInteger
andval
.- Parameters:
val
- value to be used to compute the minimum withthis
.- Returns:
min(this, val)
.- Throws:
NullPointerException
- ifval == null
.
-
max
Returns the maximum of thisBigInteger
andval
.- Parameters:
val
- value to be used to compute the maximum withthis
- Returns:
max(this, val)
- Throws:
NullPointerException
- ifval == null
-
hashCode
public int hashCode()Returns a hash code for thisBigInteger
. -
equals
Returnstrue
ifx
is a BigInteger instance and if this instance is equal to thisBigInteger
. -
toString
Returns a string representation of thisBigInteger
in decimal form. -
toString
Returns a string containing a string representation of thisBigInteger
with base radix. Ifradix < Character.MIN_RADIX
orradix > Character.MAX_RADIX
then a decimal representation is returned. The characters of the string representation are generated with methodCharacter.forDigit
.- Parameters:
radix
- base to be used for the string representation.- Returns:
- a string representation of this with radix 10.
-
gcd
Returns a newBigInteger
whose value is greatest common divisor ofthis
andval
. Ifthis==0
andval==0
then zero is returned, otherwise the result is positive.- Parameters:
val
- value with which the greatest common divisor is computed.- Returns:
gcd(this, val)
.- Throws:
NullPointerException
- ifval == null
.
-
multiply
Returns a newBigInteger
whose value isthis * val
.- Parameters:
val
- value to be multiplied withthis
.- Returns:
this * val
.- Throws:
NullPointerException
- ifval == null
.
-
pow
Returns a newBigInteger
whose value isthis ^ exp
.- Parameters:
exp
- exponent to whichthis
is raised.- Returns:
this ^ exp
.- Throws:
ArithmeticException
- ifexp < 0
.
-
divideAndRemainder
Returns aBigInteger
array which containsthis / divisor
at index 0 andthis % divisor
at index 1.- Parameters:
divisor
- value by whichthis
is divided.- Returns:
[this / divisor, this % divisor]
.- Throws:
NullPointerException
- ifdivisor == null
.ArithmeticException
- ifdivisor == 0
.- See Also:
divide(org.teavm.classlib.java.math.TBigInteger)
,remainder(org.teavm.classlib.java.math.TBigInteger)
-
divide
Returns a newBigInteger
whose value isthis / divisor
.- Parameters:
divisor
- value by whichthis
is divided.- Returns:
this / divisor
.- Throws:
NullPointerException
- ifdivisor == null
.ArithmeticException
- ifdivisor == 0
.
-
remainder
Returns a newBigInteger
whose value isthis % divisor
. Regarding signs this methods has the same behavior as the % operator on int's, i.e. the sign of the remainder is the same as the sign of this.- Parameters:
divisor
- value by whichthis
is divided.- Returns:
this % divisor
.- Throws:
NullPointerException
- ifdivisor == null
.ArithmeticException
- ifdivisor == 0
.
-
modInverse
Returns a newBigInteger
whose value is1/this mod m
. The modulusm
must be positive. The result is guaranteed to be in the interval[0, m)
(0 inclusive, m exclusive). Ifthis
is not relatively prime to m, then an exception is thrown.- Parameters:
m
- the modulus.- Returns:
1/this mod m
.- Throws:
NullPointerException
- ifm == null
ArithmeticException
- ifm < 0 or
ifthis
is not relatively prime tom
-
modPow
Returns a newBigInteger
whose value isthis^exponent mod m
. The modulusm
must be positive. The result is guaranteed to be in the interval[0, m)
(0 inclusive, m exclusive). If the exponent is negative, thenthis.modInverse(m)^(-exponent) mod m)
is computed. The inverse of this only exists ifthis
is relatively prime to m, otherwise an exception is thrown.- Parameters:
exponent
- the exponent.m
- the modulus.- Returns:
this^exponent mod val
.- Throws:
NullPointerException
- ifm == null
orexponent == null
.ArithmeticException
- ifm < 0
or ifexponent<0
and this is not relatively prime tom
.
-
mod
Returns a newBigInteger
whose value isthis mod m
. The modulusm
must be positive. The result is guaranteed to be in the interval[0, m)
(0 inclusive, m exclusive). The behavior of this function is not equivalent to the behavior of the % operator defined for the built-inint
's.- Parameters:
m
- the modulus.- Returns:
this mod m
.- Throws:
NullPointerException
- ifm == null
.ArithmeticException
- ifm < 0
.
-
isProbablePrime
public boolean isProbablePrime(int certainty)Tests whether thisBigInteger
is probably prime. Iftrue
is returned, then this is prime with a probability beyond (1-1/2^certainty). Iffalse
is returned, then this is definitely composite. If the argumentcertainty
<= 0, then this method returns true.- Parameters:
certainty
- tolerated primality uncertainty.- Returns:
true
, ifthis
is probably prime,false
otherwise.
-
nextProbablePrime
Returns the smallest integer x >this
which is probably prime as aBigInteger
instance. The probability that the returnedBigInteger
is prime is beyond (1-1/2^80).- Returns:
- smallest integer >
this
which is robably prime. - Throws:
ArithmeticException
- ifthis < 0
.
-
probablePrime
Returns a random positiveBigInteger
instance in the range [0, 2^(bitLength)-1] which is probably prime. The probability that the returnedBigInteger
is prime is beyond (1-1/2^80).Implementation Note: Currently
rnd
is ignored.- Parameters:
bitLength
- length of the newBigInteger
in bits.rnd
- random generator used to generate the newBigInteger
.- Returns:
- probably prime random
BigInteger
instance. - Throws:
IllegalArgumentException
- ifbitLength < 2
.
-