Java BigInteger setBit() Method

The setBit() method of Java BigInteger class is used to set the bit position. This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set.

Syntax:

Parameter:

n- Index of the bit to set entered by the user.

Returns:

This method returns ( this | (1<<n)).

Exception:

ArithmeticException - n is negative.

Example 1

Test it Now

Output:

SetBit operation on 5 at index 1 gives 7

Example 2

Test it Now

Output:

java.lang.ArithmeticException: Negative bit address

Example 3

Test it Now

Output:

SetBit operation on 5 at index 2 gives 5

Next TopicJava BigInteger




Latest Courses