java.math.BigInteger clearBit(int n)


On this document we will be showing a java example on how to use the clearBit(int n) method of BigInteger Class. Basically this method returns a BigInteger whose value is equivalent to this BigInteger with the designated bit cleared. (Computes (this & ~(1<<n)).)

To better illustrate the use of this method lets take for example, lets say we have a number 24 the equivalent of this number in binary would be 11000. So if we have a value n on our int as 2, then we clear the 3rd bit of 11000 which is already 0, so there would be no effect on the returned value which is still 24. Make a note that in counting start from 0 as value of n which corresponds of the rightmost bit. Moving back, if the n value provided as 3 which then clear the third bit which is 11000, the binary equivalent would then be 10000. The binary 1000 is equivalent to 16 which will be the returned value of the clearBit(3) method.


An ArithmeticException will be thrown if the specified method argument n is negative.

Method Syntax

public BigInteger clearBit(int n)

Method Argument

Data Type Parameter Description
int n index of bit to clear

Method Returns

The clearBit() method returns a BigInteger which corresponds to the result of clearing a bit designated by the method argument n.


Requires Java 1.1 and up

Java BigInteger clearBit() Example

Below is a java code demonstrates the use of clearBit() method of BigInteger class. The example presented might be simple however it shows the behavior of the clearBit() method.


import java.math.BigInteger;
import java.util.Scanner;

 * A java example source code to demonstrate
 * the use of clearBit(int n) method of BigInteger class

public class BigIntegerClearBitExample {

	public static void main(String[] args) {	
		// initialize a BigInteger object		
		BigInteger val1 = new BigInteger("19");
		 * Note: 19 in binary is 10011
		// get user input
		System.out.print("Enter the bit number you want to clear:");
		Scanner s = new Scanner(;
		String input = s.nextLine();		
		 * convert the input to Integer
		 * Autoboxing features will be used 
		 * to transform Integer to primitive int
		Integer inputBitNumber = Integer.parseInt(input);

		// get the new BigInteger 
		BigInteger resultCLearBit = val1.clearBit(inputBitNumber);
		System.out.println("Result of the operation:"+resultCLearBit);


This example is a lot simpler than it looks. We simply declare a new BigInteger with value 19. The value 19 in binary is 10011. So basically we ask the user what bit to be cleared on the our BigInteger. The result of the clearBit() operation is displayed to fully demonstrate the use of this method.

Sample Output

Below is the sample output when you run the above example.

BigInteger clearBit() example output