BigInteger setBit() in Java

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

Syntax:

BigInteger java.math.BigInteger.setBit(int n)

This method takes one argument of type int as its parameter. This method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit set. (Computes (this | (1<<n)).)

Parameters: One parameter is required for this method.

n: index of the bit to set.

Returns: this | (1<<n).

Throws: ArithmeticException - n is negative.

For Example:

BigInteger bigInteger = new BigInteger("123991194")

int n = 6

bigInteger.setBit(n) = > It returns 123991258

Approach

Java

import java.math.BigInteger;

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

        BigInteger bigInteger = new BigInteger("123991194");

        int n = 6;
        System.out.println(bigInteger.setBit(n));
    }
}

Output:

123991258

No comments:

Post a Comment