Performing Bitwise Operations with BigInteger in Java

Java 8Object Oriented ProgrammingProgramming

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 −


 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);


Result: true
Published on 20-Dec-2018 06:17:49