BigInteger testBit() in Java

testBit(): This method is available in java.math.BigInteger class of Java.

Syntax:

boolean java.math.BigInteger.testBit(int n)

This method takes one argument of int as its parameter. This method returns true if and only if the designated bit is set.(Computes ((this &(1<<n)) != 0).)

Parameters: One parameter is required for this method.

n: index of the bit to test.

Returns: true if and only if the designated bit is set..

Throws: ArithmeticException - n is negative.

For Example:

BigInteger bigInteger = new BigInteger("1234")

int n = 4

bigInteger.testBit(n) = > It returns true.

Approach

Java

import java.math.BigInteger;

public class BigIntegertestBit {
    public static void main(String[] args) {

        BigInteger bigInteger = new BigInteger("1234");
        int n = 4;
        System.out.println(bigInteger.testBit(n));
    }
}

Output:

true

No comments:

Post a Comment