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:
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final TBigIntegerTheBigIntegerconstant 1.static final TBigIntegerTheBigIntegerconstant 10.static final TBigIntegerTheBigIntegerconstant 0. -
Constructor Summary
ConstructorsConstructorDescriptionTBigInteger(byte[] val) Constructs a newBigIntegerfrom the given two's complement representation.TBigInteger(int signum, byte[] magnitude) Constructs a newBigIntegerinstance with the given sign and the given magnitude.TBigInteger(int bitLength, int certainty, Random rnd) Constructs a randomBigIntegerinstance in the range [0, 2^(bitLength)-1] which is probably prime.TBigInteger(int numBits, Random rnd) Constructs a random non-negativeBigIntegerinstance in the range [0, 2^(numBits)-1].TBigInteger(String val) Constructs a newBigIntegerinstance from the string representation.TBigInteger(String val, int radix) Constructs a newBigIntegerinstance from the string representation. -
Method Summary
Modifier and TypeMethodDescriptionabs()Returns a (new)BigIntegerwhose value is the absolute value ofthis.add(TBigInteger val) Returns a newBigIntegerwhose value isthis + val.and(TBigInteger val) Returns a newBigIntegerwhose value isthis & val.andNot(TBigInteger val) Returns a newBigIntegerwhose value isthis & ~val.intbitCount()UsebitLength(0)if you want to know the length of the binary value in bits.intReturns 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 newBigIntegerwhich has the same binary representation asthisbut with the bit at position n cleared.intcompareTo(TBigInteger val) Compares thisBigIntegerwithval.divide(TBigInteger divisor) Returns a newBigIntegerwhose value isthis / divisor.divideAndRemainder(TBigInteger divisor) Returns aBigIntegerarray which containsthis / divisorat index 0 andthis % divisorat index 1.doubleReturns thisBigIntegeras an double value.booleanReturnstrueifxis a BigInteger instance and if this instance is equal to thisBigInteger.flipBit(int n) Returns a newBigIntegerwhich has the same binary representation asthisbut with the bit at position n flipped.floatReturns thisBigIntegeras an float value.gcd(TBigInteger val) Returns a newBigIntegerwhose value is greatest common divisor ofthisandval.intReturns the position of the lowest set bit in the two's complement representation of thisBigInteger.inthashCode()Returns a hash code for thisBigInteger.intintValue()Returns thisBigIntegeras an int value.booleanisProbablePrime(int certainty) Tests whether thisBigIntegeris probably prime.longReturns thisBigIntegeras an long value.max(TBigInteger val) Returns the maximum of thisBigIntegerandval.min(TBigInteger val) Returns the minimum of thisBigIntegerandval.mod(TBigInteger m) Returns a newBigIntegerwhose value isthis mod m.Returns a newBigIntegerwhose value is1/this mod m.modPow(TBigInteger exponent, TBigInteger m) Returns a newBigIntegerwhose value isthis^exponent mod m.multiply(TBigInteger val) Returns a newBigIntegerwhose value isthis * val.negate()Returns a newBigIntegerwhose value is the-this.Returns the smallest integer x >thiswhich is probably prime as aBigIntegerinstance.not()Returns a newBigIntegerwhose value is~this.or(TBigInteger val) Returns a newBigIntegerwhose value isthis | val.pow(int exp) Returns a newBigIntegerwhose value isthis ^ exp.static TBigIntegerprobablePrime(int bitLength, Random rnd) Returns a random positiveBigIntegerinstance in the range [0, 2^(bitLength)-1] which is probably prime.remainder(TBigInteger divisor) Returns a newBigIntegerwhose value isthis % divisor.setBit(int n) Returns a newBigIntegerwhich has the same binary representation asthisbut with the bit at position n set.shiftLeft(int n) Returns a newBigIntegerwhose value isthis << n.shiftRight(int n) Returns a newBigIntegerwhose value isthis >> n.intsignum()Returns the sign of thisBigInteger.subtract(TBigInteger val) Returns a newBigIntegerwhose value isthis - val.booleantestBit(int n) Tests whether the bit at position n inthisis set.byte[]Returns the two's complement representation of this BigInteger in a byte array.toString()Returns a string representation of thisBigIntegerin decimal form.toString(int radix) Returns a string containing a string representation of thisBigIntegerwith base radix.static TBigIntegervalueOf(long val) xor(TBigInteger val) Returns a newBigIntegerwhose value isthis ^ val.Methods inherited from class java.lang.Number
byteValue, shortValue
-
Field Details
-
ZERO
TheBigIntegerconstant 0. -
ONE
TheBigIntegerconstant 1. -
TEN
TheBigIntegerconstant 10.
-
-
Constructor Details
-
TBigInteger
Constructs a random non-negativeBigIntegerinstance in the range [0, 2^(numBits)-1].- Parameters:
numBits- maximum length of the newBigIntegerin bits.rnd- is an optional random generator to be used.- Throws:
IllegalArgumentException- ifnumBitsinvalid input: '<' 0.
-
TBigInteger
Constructs a randomBigIntegerinstance in the range [0, 2^(bitLength)-1] which is probably prime. The probability that the returnedBigIntegeris prime is beyond (1-1/2^certainty).- Parameters:
bitLength- length of the newBigIntegerin bits.certainty- tolerated primality uncertainty.rnd- is an optional random generator to be used.- Throws:
ArithmeticException- ifbitLengthinvalid input: '<' 2.
-
TBigInteger
Constructs a newBigIntegerinstance 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- ifvalis not a valid representation of aBigInteger.
-
TBigInteger
Constructs a newBigIntegerinstance 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- ifvalis not a valid representation of aBigIntegeror ifradix < Character.MIN_RADIXorradix > Character.MAX_RADIX.
-
TBigInteger
public TBigInteger(int signum, byte[] magnitude) Constructs a newBigIntegerinstance 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 newBigIntegerwith 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 newBigIntegerfrom 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 newBigIntegerinstance. The given array must not be empty.- Parameters:
val- two's complement representation of the newBigInteger.- Throws:
NullPointerException- ifval == null.NumberFormatException- if the length ofvalis 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)BigIntegerwhose value is the absolute value ofthis.- Returns:
abs(this).
-
negate
Returns a newBigIntegerwhose value is the-this.- Returns:
-this.
-
add
Returns a newBigIntegerwhose value isthis + val.- Parameters:
val- value to be added tothis.- Returns:
this + val.- Throws:
NullPointerException- ifval == null.
-
subtract
Returns a newBigIntegerwhose 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:
-1ifthis < 0,0ifthis == 0,1ifthis > 0.
-
shiftRight
Returns a newBigIntegerwhose value isthis >> n. For negative arguments, the result is also negative. The shift distance may be negative which means thatthisis 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 >> nifn >= 0;this << (-n)otherwise
-
shiftLeft
Returns a newBigIntegerwhose value isthis << n. The result is equivalent tothis * 2^nif n >= 0. The shift distance may be negative which means thatthisis 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 << nifn >= 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
thiswill be at leastbitLength() + 1bits long.The value will fit into an
intifbitLength() < 32or into alongifbitLength() < 64.- Returns:
- the length of the minimal two's complement representation for
thiswithout the sign bit.
-
testBit
public boolean testBit(int n) Tests whether the bit at position n inthisis 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 inthishas to be inspected.- Returns:
this & (2^n) != 0.- Throws:
ArithmeticException- ifn < 0.
-
setBit
Returns a newBigIntegerwhich has the same binary representation asthisbut 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 inthishas to be set.- Returns:
this | 2^n.- Throws:
ArithmeticException- ifn < 0.
-
clearBit
Returns a newBigIntegerwhich has the same binary representation asthisbut 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 inthishas to be cleared.- Returns:
this & ~(2^n).- Throws:
ArithmeticException- ifn < 0.
-
flipBit
Returns a newBigIntegerwhich has the same binary representation asthisbut 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 inthishas 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,-1otherwise
-
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
thiswhich differ from the sign bit. Ifthisis positive the result is equivalent to the number of bits set in the binary representation ofthis. Ifthisis 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
thiswhich differ from the sign bit
-
not
Returns a newBigIntegerwhose 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 newBigIntegerwhose 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 newBigIntegerwhose 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 newBigIntegerwhose 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 newBigIntegerwhose 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 thisBigIntegeras an int value. Ifthisis too big to be represented as an int, thenthis% 2^32 is returned. -
longValue
public long longValue()Returns thisBigIntegeras an long value. Ifthisis too big to be represented as an long, thenthis% 2^64 is returned. -
floatValue
public float floatValue()Returns thisBigIntegeras an float value. Ifthisis too big to be represented as an float, thenFloat.POSITIVE_INFINITYorFloat.NEGATIVE_INFINITYis 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:
floatValuein classNumber- Returns:
- this
BigIntegeras a float value.
-
doubleValue
public double doubleValue()Returns thisBigIntegeras an double value. Ifthisis too big to be represented as an double, thenDouble.POSITIVE_INFINITYorDouble.NEGATIVE_INFINITYis 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:
doubleValuein classNumber- Returns:
- this
BigIntegeras a double value
-
compareTo
Compares thisBigIntegerwithval. Returns one of the three values 1, 0, or -1.- Specified by:
compareToin interfaceComparable<TBigInteger>- Parameters:
val- value to be compared withthis.- Returns:
1ifthis > val,-1ifthis < val,0ifthis == val.- Throws:
NullPointerException- ifval == null.
-
min
Returns the minimum of thisBigIntegerandval.- Parameters:
val- value to be used to compute the minimum withthis.- Returns:
min(this, val).- Throws:
NullPointerException- ifval == null.
-
max
Returns the maximum of thisBigIntegerandval.- 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
Returnstrueifxis a BigInteger instance and if this instance is equal to thisBigInteger. -
toString
Returns a string representation of thisBigIntegerin decimal form. -
toString
Returns a string containing a string representation of thisBigIntegerwith base radix. Ifradix < Character.MIN_RADIXorradix > Character.MAX_RADIXthen 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 newBigIntegerwhose value is greatest common divisor ofthisandval. Ifthis==0andval==0then 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 newBigIntegerwhose value isthis * val.- Parameters:
val- value to be multiplied withthis.- Returns:
this * val.- Throws:
NullPointerException- ifval == null.
-
pow
Returns a newBigIntegerwhose value isthis ^ exp.- Parameters:
exp- exponent to whichthisis raised.- Returns:
this ^ exp.- Throws:
ArithmeticException- ifexp < 0.
-
divideAndRemainder
Returns aBigIntegerarray which containsthis / divisorat index 0 andthis % divisorat index 1.- Parameters:
divisor- value by whichthisis divided.- Returns:
[this / divisor, this % divisor].- Throws:
NullPointerException- ifdivisor == null.ArithmeticException- ifdivisor == 0.- See Also:
-
divide
Returns a newBigIntegerwhose value isthis / divisor.- Parameters:
divisor- value by whichthisis divided.- Returns:
this / divisor.- Throws:
NullPointerException- ifdivisor == null.ArithmeticException- ifdivisor == 0.
-
remainder
Returns a newBigIntegerwhose 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 whichthisis divided.- Returns:
this % divisor.- Throws:
NullPointerException- ifdivisor == null.ArithmeticException- ifdivisor == 0.
-
modInverse
Returns a newBigIntegerwhose value is1/this mod m. The modulusmmust be positive. The result is guaranteed to be in the interval[0, m)(0 inclusive, m exclusive). Ifthisis not relatively prime to m, then an exception is thrown.- Parameters:
m- the modulus.- Returns:
1/this mod m.- Throws:
NullPointerException- ifm == nullArithmeticException- ifm < 0 orifthisis not relatively prime tom
-
modPow
Returns a newBigIntegerwhose value isthis^exponent mod m. The modulusmmust 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 ifthisis relatively prime to m, otherwise an exception is thrown.- Parameters:
exponent- the exponent.m- the modulus.- Returns:
this^exponent mod val.- Throws:
NullPointerException- ifm == nullorexponent == null.ArithmeticException- ifm < 0or ifexponent<0and this is not relatively prime tom.
-
mod
Returns a newBigIntegerwhose value isthis mod m. The modulusmmust 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 thisBigIntegeris probably prime. Iftrueis returned, then this is prime with a probability beyond (1-1/2^certainty). Iffalseis returned, then this is definitely composite. If the argumentcertaintyinvalid input: '<'= 0, then this method returns true.- Parameters:
certainty- tolerated primality uncertainty.- Returns:
true, ifthisis probably prime,falseotherwise.
-
nextProbablePrime
Returns the smallest integer x >thiswhich is probably prime as aBigIntegerinstance. The probability that the returnedBigIntegeris prime is beyond (1-1/2^80).- Returns:
- smallest integer >
thiswhich is robably prime. - Throws:
ArithmeticException- ifthis < 0.
-
probablePrime
Returns a random positiveBigIntegerinstance in the range [0, 2^(bitLength)-1] which is probably prime. The probability that the returnedBigIntegeris prime is beyond (1-1/2^80).Implementation Note: Currently
rndis ignored.- Parameters:
bitLength- length of the newBigIntegerin bits.rnd- random generator used to generate the newBigInteger.- Returns:
- probably prime random
BigIntegerinstance. - Throws:
IllegalArgumentException- ifbitLength < 2.
-