Java.math package extras
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Java.math.BigInteger Class
Introduction
The java.math.BigInteger class provides operations analogues to all of Java s primitive integer operators and for all relevant methods from java.lang.Math.
It also provides operations for modular arithmetic, GCD calculation, primapty testing, prime generation, bit manipulation, and a few other miscellaneous operations. All operations behave as if BigIntegers were represented in two s-complement notation.
Semantics of arithmetic operations and bitwise logical operations are similar to those of Java s integer arithmetic operators and Java s bitwise integer operators respectively. Semantics of shift operations extend those of Java s shift operators to allow for negative shift distances.
Comparison operations perform signed integer comparisons. Modular arithmetic operations are provided to compute residues, perform exponentiation, and compute multippcative inverses. Bit operations operate on a single bit of the two s-complement representation of their operand.
All methods and constructors in this class throw NullPointerException when passed a null object reference for any input parameter.
Class declaration
Following is the declaration for java.math.BigInteger class −
pubpc class BigInteger extends Number implements Comparable<BigInteger>
Field
Following are the fields for java.math.BigInteger class −
static BigInteger ONE − The BigInteger constant one.
static BigInteger TEN − The BigInteger constant ten.
static BigInteger ZERO − The BigInteger constant zero.
Class constructors
Sr.No. | Constructor & Description |
---|---|
1 | BigInteger(byte[] val) This constructor is used to translate a byte array containing the two s-complement binary representation of a BigInteger into a BigInteger. |
2 | BigInteger(int signum, byte[] magnitude) This constructor is used to translate the sign-magnitude representation of a BigInteger into a BigInteger. |
3 | BigInteger(int bitLength, int certainty, Random rnd) This constructor is used to construct a randomly generated positive BigInteger that is probably prime, with the specified bitLength. |
4 | BigInteger(int numBits, Random rnd) This constructor is used to construct a randomly generated BigInteger, uniformly distributed over the range 0 to (2numBits - 1), inclusive. |
5 | BigInteger(String val) This constructor is used to translate the decimal String representation of a BigInteger into a BigInteger. |
6 | BigInteger(String val, int radix) This constructor is used to translate the String representation of a BigInteger in the specified radix into a BigInteger. |
Class methods
Sr.No. | Method & Description |
---|---|
1 | This method returns a BigInteger whose value is the absolute value of this BigInteger. |
2 | This method returns a BigInteger whose value is (this + val). |
3 | This method returns a BigInteger whose value is (this & val). |
4 | This method returns a BigInteger whose value is (this & ~val). |
5 | This method returns the number of bits in the two s complement representation of this BigInteger that differ from its sign bit. |
6 | This method returns the number of bits in the minimal two s-complement representation of this BigInteger, excluding a sign bit. |
7 | This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared. |
8 | This method compares this BigInteger with the specified BigInteger. |
9 | This method returns a BigInteger whose value is (this / val). |
10 | This method returns an array of two BigIntegers containing (this / val) followed by (this % val). |
11 | This method converts this BigInteger to a double. |
12 | This method compares this BigInteger with the specified Object for equapty. |
13 | This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit fppped. |
14 | This method converts this BigInteger to a float. |
15 | This method returns a BigInteger whose value is the greatest common spanisor of abs(this) and abs(val). |
16 | This method returns the index of the rightmost (lowest-order) one bit in this BigInteger (the number of zero bits to the right of the rightmost one bit). |
17 | This method returns the hash code for this BigInteger. |
18 | This method converts this BigInteger to an int. |
19 | This method returns true if this BigInteger is probably prime, false if it s definitely composite. |
20 | This method converts this BigInteger to a long. |
21 | This method returns the maximum of this BigInteger and val. |
22 | This method returns the minimum of this BigInteger and val. |
23 | This method returns a BigInteger whose value is (this mod m). |
24 | This method returns a BigInteger whose value is (this-1 mod m). |
25 | This method returns a BigInteger whose value is (thisexponent mod m). |
26 | This method returns a BigInteger whose value is (this * val). |
27 | This method returns a BigInteger whose value is (-this). |
28 | This method returns the first integer greater than this BigInteger that is probably prime. |
29 | This method returns a BigInteger whose value is (~this). |
30 | This method returns a BigInteger whose value is (this | val). |
31 | This method returns a BigInteger whose value is (thisexponent). |
32 | This method returns a positive BigInteger that is probably prime, with the specified bitLength. |
33 | This method returns a BigInteger whose value is (this % val). |
34 | This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set. |
35 | This method returns a BigInteger whose value is (this << n). |
36 | This method returns a BigInteger whose value is (this >> n). |
37 | This method returns the signum function of this BigInteger. |
38 | This method returns a BigInteger whose value is (this - val). |
39 | This method returns true if and only if the designated bit is set. |
40 | This method returns a byte array containing the two s-complement representation of this BigInteger. |
41 | This method returns the decimal String representation of this BigInteger. |
42 | This method returns the String representation of this BigInteger in the given radix. |
43 | This method returns a BigInteger whose value is equal to that of the specified long. |
44 | This method returns a BigInteger whose value is (this ^ val). |