Java BigInteger Class | clearBit() Method with Example

BigInteger Class clearBit() method: Here, we are going to learn about the clearBit() method of BigInteger Class with its syntax and example.
Submitted by Preeti Jain, on May 10, 2020

BigInteger Class clearBit() method

  • clearBit() method is available in java.math package.
  • clearBit() method is used to clear the set bit at the given indices from this BigInteger if it exists.
  • clearBit() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • clearBit() method does not throw an exception at the time of clearing particular bits.
    ArithmeticException: This exception may throw when the given parameter value is less than 0.

Syntax:

    public BigInteger clearBit(int indices);

Parameter(s):

  • int indices represents the bit index to clear.

Return value:

The return type of this method is BigInteger, it returns BigInteger and its value is calculated by using [(this BigInteger) & ~(1<< indices)].

Example:

// Java program to demonstrate the example 
// of clearBit(int indices) method of BigInteger

import java.math.*;

public class ClearBitOfBI {
    public static void main(String args[]) {
        // Initialize a variable str
        String str = "10";
        
        // Initialize a BigInteger object 
        BigInteger b_int = new BigInteger(str);

        // Display b_int
        System.out.println("b_int: " + b_int);

        // Display Binary representation of str 
        System.out.println("Binary Representation of 10: 1010 ");

        // clears the set bit value "1" at the
        // given indices (1) from this BigDecimal 
        // b_int
        BigInteger bit_clear = b_int.clearBit(1);
        System.out.println("b_int.clearBit(1): " + bit_clear);
    }
}

Output

b_int: 10
Binary Representation of 10: 1010 
b_int.clearBit(1): 8


Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.