Java BitSet set() method

The set() method of Java BitSet class is used to set the bit value to true at the specified index. There are various overloaded set() method available in BitSet class. These methods are differentiated on the basis of their parameters.

Syntax:

Parameter:

DataTypeParameterDescription
intbitIndexIt is an index of bit.
booleanvalueIt is Boolean value to set true of false
intfromIndexIt is the index of a bit from which the set of bit starts.
inttoIndexIt is the exclusive index of a bit at which the set of bit ends.

Returns:

NA

Exception:

This method throws IndexOutOfBoundsException if any of the following statements became true:

  • If the specified index is negative.
  • If the specified index fromIndex is greater than the index toIndex.

Compatibility Version:

MethodCompatibility Version
set(int bitIndex)Java 1.0 and above
set(int bitIndex, boolean value)Java 1.4 and above
set(int fromIndex, int toIndex)Java 1.4 and above
set(int fromIndex, int toIndex, boolean value)Java 1.4and above

Example of Java BitSet set(int bitIndex) method

Example 1

Output:

true bit value at index: {0, 2, 3, 5, 6}

Example of Java BitSet set(int bitIndex, boolean value) method

Example 2

Output:

true bit value at index: {0, 1, 5}

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

Example 3

Output:

true bit value at index: {0, 1, 4, 7, 8}

Example of Java BitSet set(int fromIndex, int toIndex, boolean value) method

Example 3

Output:

true bit value at index: {0, 1, 4, 10, 11}