BigInteger flipBit() in Java

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

Syntax:

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

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

Parameters: One parameter is required for this method.

n: index of the bit to flip.

Returns: this ^ (1<<n).

Throws: ArithmeticException - n is negative.

For Example:

BigInteger bigInteger = new BigInteger("1234")

int n = 4

bigInteger.flipBit(n) = > It returns 1218.

Approach

Java

import java.math.BigInteger;

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

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

Output:

1218

No comments:

Post a Comment