- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
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 |
---|---|
1 | BigInteger abs() This method returns a BigInteger whose value is the absolute value of this BigInteger. |
2 | BigInteger add(BigInteger val) This method returns a BigInteger whose value is (this + val). |
3 | BigInteger and(BigInteger val) This method returns a BigInteger whose value is (this & val). |
4 | int bitCount() This method returns the number of bits in the two's complement representation of this BigInteger that differ from its sign bit. |
5 | int compareTo(BigInteger val) This method compares this BigInteger with the specified BigInteger. |
Example
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
- Related Articles
- BigInteger Class in C#
- Multiply one BigInteger to another BigInteger in Java
- Subtract one BigInteger from another BigInteger in Java
- Divide one BigInteger from another BigInteger in Java
- Negate a BigInteger in Java
- Create BigInteger via string in Java
- Working with BigInteger Values in Java
- Math Operations on BigInteger in Java
- Shift right in a BigInteger in Java
- Shift left in a BigInteger in Java
- Create BigInteger from byte array in Java
- Performing Bitwise Operations with BigInteger in Java
- Get byte array from BigInteger in Java
- Set a bit for BigInteger in Java
- Clear a bit in a BigInteger in Java
