Java BitSet nextClearBit() method

The nextClearBit(int fromIndex) method of Java BitSet class returns the index of first bit which is set to false that occurs on or after the specified index.

Syntax:

Parameter:

DataTypeParameterDescription
intfromIndexIt is an index of BitSet from which the checking of clear bit starts.

Returns:

This method returns the index of the next clear bit.

Exception:

IndexOutOfBoundsException - throw an exception if the specified index is negative.

Compatibility Version:

Java 1.4 and above

Example 1

Output:

bitset: {0, 1, 3, 4}
clear bit next to 3: 5
clear bit next to 0: 2
clear bit next to 2: 2

Example 2

The nextClearBit(int fromIndex) method throws an exception if the specified index is negative.

Output:

Exception in thread "main" java.lang.IndexOutOfBoundsException: fromIndex < 0: -1
	at java.util.BitSet.nextClearBit(Unknown Source)
	at BitSetNextClearBitExample2.main(BitSetNextClearBitExample2.java:14)
bitset: {0, 1, 3, 4}