Tuesday, October 7, 2025
HomeLanguagesJavaBitSet nextClearBit() method in Java

BitSet nextClearBit() method in Java

BitSet is a class defined in the java.util package. It creates an array of bits represented by boolean values.

Prerequisite : Java BitSet | Set 1

nextClearBit() method:
This method in BitSet class is used to return the index of the first bit that is set to false that occurs on or after the specified starting index.

Syntax:

public int nextClearBit(int fromIndex)

Parameters: This method takes a mandatory parameter fromIndex which is the index to start checking from (inclusive) for the next false bit.

Return Value: This method returns the index of the next clear bit, i.e. the index of the first bit that is set to false that occurs on or after the specified starting index.

Exceptions: This method throws IndexOutOfBoundsException if the specified index is negative.

Example 1: To show the implementation of nextClearBit() function :




// Java program illustrating Bitset
// nextClearBit() function.
  
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
        // Constructors of BitSet class
        BitSet bs1 = new BitSet();
        BitSet bs2 = new BitSet();
  
        /* assigning values to set1*/
        bs1.set(0);
        bs1.set(1);
        bs1.set(2);
        bs1.set(4);
  
        // assign values to bs2
        bs2.set(4);
        bs2.set(6);
        bs2.set(5);
        bs2.set(1);
        bs2.set(2);
        bs2.set(3);
        bs2.set(12);
  
        // Printing the 2 Bitsets
        System.out.println("bs1 : " + bs1);
        System.out.println("bs2 : " + bs2);
  
        // Print the first clear bit of bs1
        System.out.println(bs1.nextClearBit(0));
  
        // Print the first clear bit of bs2 after index 3
        System.out.println(bs2.nextClearBit(3));
    }
}


Output:

bs1 : {0, 1, 2, 4}
bs2 : {1, 2, 3, 4, 5, 6, 12}
3
7

Example 2: To show IndexOutOfBoundException:




// Java program illustrating Bitset
// nextClearBit() function.
  
import java.util.*;
public class GFG {
    public static void main(String[] args)
    {
        // Constructors of BitSet class
        BitSet bs1 = new BitSet();
        BitSet bs2 = new BitSet();
  
        /* assigning values to set1*/
        bs1.set(0);
        bs1.set(1);
        bs1.set(2);
        bs1.set(4);
  
        // assign values to bs2
        bs2.set(4);
        bs2.set(6);
        bs2.set(5);
        bs2.set(1);
        bs2.set(2);
        bs2.set(3);
        bs2.set(12);
  
        // Printing the 2 Bitsets
        System.out.println("bs1 : " + bs1);
        System.out.println("bs2 : " + bs2);
  
        try {
            // Passing -1 as parameter
            System.out.println(bs1.nextClearBit(-1));
        }
        catch (Exception e) {
            System.out.println("Exception when "
                               + "negative index is passed "
                               + "as parameter : " + e);
        }
    }
}


Output:

bs1 : {0, 1, 2, 4}
bs2 : {1, 2, 3, 4, 5, 6, 12}
Exception when negative index is passed as parameter : java.lang.IndexOutOfBoundsException: fromIndex < 0: -1
RELATED ARTICLES

Most Popular

Dominic
32340 POSTS0 COMMENTS
Milvus
86 POSTS0 COMMENTS
Nango Kala
6708 POSTS0 COMMENTS
Nicole Veronica
11872 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11936 POSTS0 COMMENTS
Shaida Kate Naidoo
6829 POSTS0 COMMENTS
Ted Musemwa
7090 POSTS0 COMMENTS
Thapelo Manthata
6780 POSTS0 COMMENTS
Umr Jansen
6784 POSTS0 COMMENTS