This method in BitSet class is used to return the index of the first bit that is set to false
import java.util.BitSet;
public class BitSetDemo {
public static void main(String[] args) {
BitSet b = new BitSet();
b.set(5);
b.set(9);
b.set(6);
System.out.println(""+b);
System.out.println(b.nextClearBit(5));
System.out.println(b.nextClearBit(9));
}
}
Output :
{5, 6, 9}
7
10
In this code, 6 is set after 9 but it shows that the values are stored consecutively ((b.nextClearBit(5) returns next value which is 7). So, how BitSet store these values ?
The javadoc of nextClearBit
says:
Returns the index of the first bit that is set to
false
that occurs on or after the specified starting index.
You have set 5, 6 and 9 to true. That means that starting from 5, the first index set to false is 7. And starting from 9, the first index set false is 10. Which according to your own output is also what is returned.
If you want to know how BitSet
works and what it does, read its Javadoc and look at the source. It is included with the JDK.