BigInteger class in Javan



The java.math.BigInteger class provides operations analogs 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, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations. All operations behave as if BigIntegers were represented in twos-complement notation.

The 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. The 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 multiplicative inverses. Bit operations operate on a single bit of the twos-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 −

public 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.

Important Methods

Sr.No.
Method & Description
1BigInteger abs()
This method returns a BigInteger whose value is the absolute value of this BigInteger.

2BigInteger add(BigInteger val)
This method returns a BigInteger whose value is (this + val).

3BigInteger and(BigInteger val)
This method returns a BigInteger whose value is (this & val).

4int bitCount()
This method returns the number of bits in the two's complement representation of this BigInteger that differ from its sign bit.

5int compareTo(BigInteger val)
This method compares this BigInteger with the specified BigInteger.

Example

Live Demo

import java.math.BigInteger;

public class Tester {
   public static void main(String[] args) {
      // create 3 BigInteger objects
      BigInteger bi1, bi2;
 
      // assign values to bi1, bi2
      bi1 = new BigInteger("123");
      bi2 = new BigInteger("-50");

      System.out.println("Absolute value of "          
         + bi2 + " is " + bi2.abs());
      System.out.println("Result of addition of "          
         + bi1 + ", "+ bi2 + " is " +bi1.add(bi2));
      System.out.println("Result of and opearation of "
         + bi1 + ", "+ bi2 + " is " +bi1.and(bi2));
      System.out.println("Result of bitCount opearation of "
         + bi1 + " is " +bi1.bitCount());
      System.out.println("Result of compareTo opearation of "
         + bi1 + ", "+ bi2 + " is " +bi1.compareTo(bi2));
   }
}

Output

Absolute value of -50 is 50
Result of addition of 123, -50 is 73
Result of and opearation of 123, -50 is 74
Result of bitCount opearation of 123 is 6
Result of compareTo opearation of 123, -50 is 1
Samual Sam
Samual Sam

Learning faster. Every day.


Advertisements