Prerequisite: BigInteger Basics
The clearBit() method returns a BigInteger which is used to clear a particular bit position in a BigInteger. The bit at index n of binary representaion of BigInteger will be cleared means converted to zero. Mathematically we can say that it is used to calculate this & ~(1<<n).
Syntax:
public BigInteger clearBit(int n)
Parameter: The method takes one parameter n which refers to the index of the bit needed to be cleared.
Return Value: The method returns the BigInteger after clearing the bit position n.
Throws: The method might throw an ArithmeticException when n is negative.
Examples:
Input: value = 2300, index = 3 Output: 2292 Explanation: Binary Representation of 2300 = 100011111100 bit at index 3 is 1 so clear the bit at index 3 Now Binary Representation becomes 100011110100 and Decimal equivalent of 100011110100 is 2292 Input: value = 5482549, index = 0 Output: 5482548
Below program illustrate the clearBit(index) method of BigInteger().
/* *Program Demonstrate clearBit() method of BigInteger */import java.math.*; public class GFG { public static void main(String[] args) { // Creating BigInteger object BigInteger biginteger = new BigInteger("5482549"); // Creating an int i for index int i = 0; // Call clearBit() method on bigInteger at index i // store the return BigInteger BigInteger changedvalue = biginteger.clearBit(i); String result = "After applying clearbit at index " + i + " of " + biginteger+" New Value is " + changedvalue; // Print result System.out.println(result); } } |
After applying clearbit at index 0 of 5482549 New Value is 5482548
Reference:https://docs.oracle.com/javase/7/docs/api/java/math/BigInteger.html#clearBit(int)
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- BigInteger pow() Method in Java
- BigInteger mod() Method in Java
- BigInteger xor() Method in Java
- BigInteger and() Method in Java
- BigInteger abs() Method in Java
- BigInteger or() method in Java
- BigInteger not() Method in Java
- Java.math.BigInteger.probablePrime() method in Java
- Java.math.BigInteger.modInverse() method in Java
- BigInteger toString() Method in Java
- BigInteger getLowestSetBit() Method in Java
- BigInteger testBit() Method in Java
- BigInteger negate() Method in Java
- BigInteger andNot() Method in Java
- BigInteger longValue() Method in Java
- BigInteger intValue() Method in Java
- BigInteger floatValue() Method in Java
- BigInteger doubleValue() Method in Java
- BigInteger flipBit() Method in Java
- BigInteger remainder() Method in Java
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

