Performing Bitwise Operations with BigInteger in Java


BigInteger class is used for big integer calculations which are outside the limit of the primitive data types. It provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations.

Let us work with the testBit() method in Java to perform Bitwise operation. The java.math.BigInteger.testBit(int n) returns true if and only if the designated bit is set −

The following is an example −

Example

 Live Demo

import java.math.*;
public class BigIntegerDemo {
   public static void main(String[] args) {
      BigInteger one;
      Boolean two;
      one = new BigInteger("5");
      two = one.testBit(2);
      System.out.println("Result: " +two);
   }
}

Output

Result: true

Updated on: 29-Jun-2020

209 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements