Java BigInteger testBit() Method

The testBit() method of Java BigInteger class is used to check whether the designated bit is set or not. This method returns true if and only if the designated bit is set. This method computes (this & (1<<n)) != 0).

Syntax:

Parameter:

n- index of the bit to test

Returns:

The method returns true if and only if the designated bit is set else it will return false.

Exception:

This method throws an ArithmeticException when n is negative.

Example 1

Test it Now

Output:

Test Bit on 5 at index 2 returns true
Test Bit on 5 at index 3 returns false

Example 2

Test it Now

Output:

java.lang.ArithmeticException: Negative bit address

Next TopicJava BigInteger




Latest Courses