What is BitSet.set() in Java?

set() is an instance method of BitSet that is used to set the bit at the specified position in the BitSet object.

The four variations of the method are as follows:

  • set(int bitIndex)
  • set(int bitIndex, boolean value)
  • set(int fromIndex, int toIndex)
  • set(int fromIndex, int toIndex, boolean value)

The set method is defined in the BitSet class. The BitSet class is defined in the java.util package. To import the BitSet class, use the following import statement.

import java.util.BitSet;

The set(int bitIndex) method

The set(int bitIndex) method is used to set the bit at the given index.

Syntax


public void set(int bitIndex)

Parameters

  • int bitIndex: The bit index.

Return value

This method does not return anything.

Code

import java.util.BitSet;
public class Main{
public static void main(String[] args) {
// Create empty BitSet object
BitSet bitSet = new BitSet();
// Set the index 2
bitSet.set(2);
// Set the index 3
bitSet.set(3);
// Print the BitSet object
System.out.println("BitSet using set(index) method = " + bitSet);
}
}

The set(int bitIndex, boolean value) method

The set(int bitIndex, boolean value) method is used to set the bit at the given index to the specified value.

Syntax


public void set(int bitIndex, boolean value)

Parameters

  • int bitIndex: The bit index.
  • boolean value: The boolean value to set.

Return value

This method does not return anything.

Code

import java.util.BitSet;
public class Main{
public static void main(String[] args) {
// Create empty BitSet object
BitSet bitSet = new BitSet();
// Set the index 2 to value 0
bitSet.set(2, false);
// Set the index 3 to value 1
bitSet.set(3, true);
// Print the BitSet object
System.out.println("BitSet using set(index, value) method = " + bitSet);
}
}

The set(int fromIndex, int toIndex) method

The set(int fromIndex, int toIndex) method is used to set the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to true.

Syntax


public void set(int fromIndex, int toIndex)

Parameters

  • int fromIndex: The index of the first bit to set.
  • int toIndex: The index of the last bit to set.

Return value

This method does not return anything.

Code

import java.util.BitSet;
public class Main{
public static void main(String[] args) {
// Create empty BitSet object
BitSet bitSet = new BitSet();
// Set the bits from index 2 to 5
bitSet.set(2, 5);
// Print the BitSet object
System.out.println("BitSet using set(fromIndex, toIndex) method = " + bitSet);
}
}

The set(int fromIndex, int toIndex, boolean value) method

The set(int fromIndex, int toIndex, boolean value) method is used to set the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to the specified value.

Syntax


public void set(int fromIndex, int toIndex, boolean value)

Parameters

  • int fromIndex: The index of the first bit to set.
  • int toIndex: The index of the last bit to set.
  • boolean value: The boolean value to set.

Return value

This method does not return anything.

Code

import java.util.BitSet;
public class Main{
public static void main(String[] args) {
// Create empty BitSet object
BitSet bitSet = new BitSet();
// Set the bits from index 2 to 5 to value 0
bitSet.set(2, 5, false);
// Set the bits from index 8 to 10 to value 1
bitSet.set(8, 10, true);
// Print the BitSet object
System.out.println("BitSet using set(fromIndex, toIndex, value) method = " + bitSet);
}
}