Java Utility Library

Java BitSet - nextClearBit() Method



The java.util.BitSet.nextClearBit() method returns 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

fromIndex Specify the index to start checking from (inclusive).

Return Value

Returns the index of the next clear bit.

Exception

Throws IndexOutOfBoundsException, if the specified index is negative.

Example:

In the example below, the java.util.BitSet.nextClearBit() method returns the index of the first bit which is set to false and occurs on or after the specified starting index in the given BitSet.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {
    //creating a BitSet
    BitSet BSet = new BitSet();

    //populating BitSet
    BSet.set(1);
    BSet.set(2);
    BSet.set(3);
    BSet.set(4);
    BSet.set(10); 

    //printing BitSet
    System.out.println("BSet contains: " + BSet);

    //finding index of the bit which is set to false
    //which occurs on or after index = 2
    System.out.print("bit which is set to false on or after index 2 is: ");
    System.out.print(BSet.nextClearBit(2));
  }
}

The output of the above code will be:

BSet contains: {1, 2, 3, 4, 10}
bit which is set to false on or after index 2 is: 5

❮ Java.util - BitSet