Java BitSet previousClearBit() method

The previousClearBit(int fromIndex) method of Java BitSet class returns the index of the nearest bit which is set to false (clear) that occurs on or before the specified index. It returns -1 if the specified index is negative or no such clear bit exists in the BitSet.

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 previous clear bit, or -1 if there is no such clear bit exists.

Exception:

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

Compatibility Version:

Java 1.7 and above

Example 1

Output:

bitset: {0, 1, 3, 4}
previous clear bit on or before 1: -1
previous clear bit on or before 4: 2
previous clear bit on or before 2: 2

Example 2

The previousClearBit(int fromIndex) method returns -1 if the specified index is negative.

Output:

bitset: {0, 1, 3, 4}
clear bit previous to -1: -1