Java.math - SPLessons

Java.math BigInteger

Home > Lesson > Chapter 3
SPLessons 5 Steps, 3 Clicks
5 Steps - 3 Clicks

Java.math BigInteger

Java.math BigInteger

shape Description

The Java.math BigInteger gives operations analogues to integer operators and for methods from java.lang.Math. Following is the declaration for java.math.BigInteger class. [java] public class BigInteger extends Number implements Comparable<BigInteger> [/java] The functionality of java.lang.Math class is to perform basic operations such as addition, subtraction, square root, logarithmic and trigonometric functions. Following is the syntax declaration of this class. [java] public final class Math extends Object [/java] Following are the basic methods of java.lang.Math class.
Methods Description
abs(double) Absolute value of a double value will be returned.
abs(float) Absolute value of a float value will be returned.
abs(int) Absolute value of a int value will be returned.
acos(double) To return the cosine of a value, in the range of 0.0 through pi.
asin(double) To return the sine of a value, in the range of-pi/2 through pi/2.
ceil(double) To return the smallest double value.
floor(double) To return the greatest double value.
sqrt(double) To return the square root for the double value.

Methods Of java.math.BigInteger Class

shape Methods

Following are the regular using methods of java.math.BigInteger Class.
Methods Description
abs() To return the absolute value of the BigInteger.
and(BigInteger) To return a BigInteger whose value is (this & val).
bitLength() To give back the quantity of bits in the negligible two's-supplement representation of this BigInteger, barring a sign bit.
clearBit(int) To give back a BigInteger whose esteem is proportional to this number with the designated bit cleared.
hashCode() To get the hashcode of the number.
remainder(BigInteger) To get the remainder of the value.

By Using java.math.BigInteger.abs() Method

shape Description

The functionality of the java.math.BigInteger.abs() method is to return the absolute value of the BigInteger. Following is the declaration of the method. [java]public BigInteger abs()[/java] DemoBigInteger.java [java] package com.splessons; import java.math.*; public class DemoBigInteger { public static void main(String[] args) { // create 4 BigInteger objects BigInteger bi1, bi2, bi3, bi4; // assign values to bi1, bi2 bi1 = new BigInteger("12345"); bi2 = new BigInteger("-12345"); // assign absolute values of bi1, bi2 to bi3, bi4 bi3 = bi1.abs(); bi4 = bi2.abs(); String str1 = "Absolute value of " + bi1 + " is " + bi3; String str2 = "Absolute value of " + bi2 + " is " + bi4; // print bi3, bi4 values System.out.println( str1 ); System.out.println( str2 ); } } [/java] Output: Now compile the code result will be as follows. [java] Absolute value of 12345 is 12345 Absolute value of -12345 is 12345 [/java]

By Using java.math.BigInteger.add() Method

shape Description

The functionality of the java.math.BigInteger.add() method is to return a BigInteger whose value is (this & val). Following is the declaration of the method. [java]public BigInteger add(BigInteger val)[/java] DemoBigInteger.java [java] package com.splessons; import java.math.*; public class DemoBigInteger { public static void main(String[] args) { // create 3 BigInteger objects BigInteger bi1, bi2, bi3; // assign values to bi1, bi2 bi1 = new BigInteger("143"); bi2 = new BigInteger("341"); // perform add operation on bi1 using bi2 bi3 = bi1.add(bi2); String str = "Result of addition is " +bi3;; // print bi3 value System.out.println( str ); } } [/java] In the above example, the addition of two values will be printed and bi3 is used to add bi1 and bi2. Output: Now compile the code result will be as follows. [java] Result of addition is 484 [/java]

By Using java.math.BigInteger.bitLength() Method

shape Description

The functionality of the java.math.BigInteger.bitLength() method is to give back the quantity of bits in the negligible two’s-supplement representation of this BigInteger, barring a sign bit. Following is the declaration of the method. [java]public int bitLength()[/java] DemoBigInteger.java [java] package com.splessons; import java.math.*; public class DemoBigInteger { public static void main(String[] args) { // create 2 BigInteger objects BigInteger bi1, bi2; // create 2 int objects int i1, i2; // assign values to bi1, bi2 bi1 = new BigInteger("8"); bi2 = new BigInteger("-8"); // perform bitlength operation on bi1, bi2 i1 = bi1.bitLength(); i2 = bi2.bitLength(); String str1 = "Result of bitlength operation on " + bi1 +" is " +i1; String str2 = "Result of bitlength operation on " + bi2 +" is " +i2; // print i1, i2 values System.out.println( str1 ); System.out.println( str2 ); } } [/java] In the above example, the developer has taken two integers and assigned values such as 8 and -8, the bit length of those valued will be printed by using the bitlength() method. Output: Now compile the code result will be as follows. [java] Result of bitlength operation on 8 is 4 Result of bitlength operation on -8 is 3 [/java]

By Using java.math.BigInteger.clearBit() Method

shape Description

The functionality of the java.math.BigInteger.clearBit() method is to give back a BigInteger whose esteem is proportional to this number with the designated bit cleared. Following is the declaration of the method. [java]public BigInteger clearBit(int n)[/java] DemoBigInteger.java [java] package com.splessons; import java.math.*; public class DemoBigInteger { public static void main(String[] args) { // create 2 BigInteger objects BigInteger bi1, bi2; // assign value to bi1 bi1 = new BigInteger("9"); // perform clearbit operation on bi1 with index 2 bi2 = bi1.clearBit(7); String str = "Clearbit operation on " +bi1+ " at index 7 gives " +bi2; // print bi2 value System.out.println( str ); } } [/java] Output: Now compile the code result will be as follows. [java] Clearbit operation on 9 at index 7 gives 9 [/java]

By Using java.math.BigInteger.hashCode() Method

shape Description

The functionality of the java.math.BigInteger.hashCode() method is to get the hashcodeof the number. Following is the declaration of the method. [java]public int hashCode()[/java] DemoBigInteger.java [java] package com.splessons; import java.math.*; public class DemoBigInteger { public static void main(String[] args) { // create 2 BigInteger objects BigInteger bi1, bi2; // create 2 int objects int i1, i2; // assign values to bi1, bi2 bi1 = new BigInteger("51234"); bi2 = new BigInteger("789123923456"); // assign the hashcode of bi1, bi2 to i1, i2 i1 = bi1.hashCode(); i2 = bi2.hashCode(); String str1 = "HashCode of " +bi1+ " is " +i1; String str2 = "HashCode of " +bi2+ " is " +i2; // print i1, i2 values System.out.println( str1 ); System.out.println( str2 ); } } [/java] In the above example, the values of the hashcode will be printed. Output: Now compile the code result will be as follows. [java] HashCode of 51234 is 51234 HashCode of 789123923456 is -1150053335 [/java]

By Using java.math.BigInteger.remainder() Method

shape Description

The functionality of the java.math.BigInteger.remainder()() method is to get the remainder of the value. Following is the declaration of the method. [java]public BigInteger remainder(BigInteger val)[/java] DemoBigInteger.java [java] package com.splessons; import java.math.*; public class DemoBigInteger { public static void main(String[] args) { // create 3 BigInteger objects BigInteger bi1, bi2, bi3; bi1 = new BigInteger("-1000"); bi2 = new BigInteger("35"); // perform remainder operation on bi1 using bi2 bi3 = bi1.remainder(bi2); String str = bi1 + " % " + bi2 + " is " +bi3; // print bi3 value System.out.println("Remainder result for " +str); } } [/java] In the above example, the developer has given one negative value and positive value to find the remainder. Output: Now compile the code result will be as follows. [java] Remainder result for -1000 % 35 is -20 [/java]

Summary

shape Key Points

  • Java.math BigInteger - The java.math.BigInteger.shiftLeft() method is used to return BigInteger value as <<n .
  • Java.math BigInteger - The java.math.BigInteger.setBit() method gives back a BigInteger whose esteem is proportional to this BigInteger with the assigned bit set.