Java BitSet clear() method

The clear() method of Java BitSet class is used to set the bits in the BitSet to false. There are various overloaded clear() methods available in BitSet class.

1. Java BitSet clear() method

The clear() method set all the bits in the BitSet to false.

2. Java BitSet clear(int bitIndex) method

The clear(int bitIndex) method set the bit of specified index to false.

3. Java BitSet clear(int fromIndex, int toIndex) method

The clear(int fromIndex, int toIndex) method set the bits to false from specified inclusive fromIndex to exclusive toIndex.

Syntax:

Parameter:

DataTypeParameterDescription
intbitIndexIt is the index of bit value
intfromIndexIt is the index of bit from which the false value of bit starts.
inttoIndexIt is the index of bit at which the false value of bit ends.

Returns:

NA

Exception:

MethodException
clear()NA
clear(int bitIndex)IndexOutOfBoundsException - if the specified index is negative
clear(int fromIndex, int toIndex)IndexOutOfBoundsException - if any of the specified fromIndex or toIndex is negative or fromIndex is larger than the toIndex.

Compatibility Version:

MethodCompatibility Version
clear()Java 1.4 and above
clear(int bitIndex)Java 1.0 and above
clear(int fromIndex, int toIndex)Java 1.4 and above

Example of Java BitSet clear() method

Example 1

Output:

bitset: {0, 1, 2, 3, 4}
result bitset: {}

Example of Java BitSet clear(int bitIndex) method

Example 2

Output:

bitset: {5, 10, 15, 20, 25}
result bitset: {5, 15, 20, 25}

Example 3

Example of Java BitSet clear(int fromIndex, int toIndex) method

Output:

bitset: {5, 10, 15, 20, 25}
result bitset: {5, 25}