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;
set(int bitIndex)
methodThe set(int bitIndex)
method is used to set the bit at the given index.
public void set(int bitIndex)
int bitIndex
: The bit index.This method does not return anything.
import java.util.BitSet;public class Main{public static void main(String[] args) {// Create empty BitSet objectBitSet bitSet = new BitSet();// Set the index 2bitSet.set(2);// Set the index 3bitSet.set(3);// Print the BitSet objectSystem.out.println("BitSet using set(index) method = " + bitSet);}}
set(int bitIndex, boolean value)
methodThe set(int bitIndex, boolean value)
method is used to set the bit at the given index to the specified value.
public void set(int bitIndex, boolean value)
int bitIndex
: The bit index.boolean value
: The boolean value to set.This method does not return anything.
import java.util.BitSet;public class Main{public static void main(String[] args) {// Create empty BitSet objectBitSet bitSet = new BitSet();// Set the index 2 to value 0bitSet.set(2, false);// Set the index 3 to value 1bitSet.set(3, true);// Print the BitSet objectSystem.out.println("BitSet using set(index, value) method = " + bitSet);}}
set(int fromIndex, int toIndex)
methodThe set(int fromIndex, int toIndex)
method is used to set the bits from the specified fromIndex
(inclusive) to the specified toIndex
(exclusive) to true
.
public void set(int fromIndex, int toIndex)
int fromIndex
: The index of the first bit to set.int toIndex
: The index of the last bit to set.This method does not return anything.
import java.util.BitSet;public class Main{public static void main(String[] args) {// Create empty BitSet objectBitSet bitSet = new BitSet();// Set the bits from index 2 to 5bitSet.set(2, 5);// Print the BitSet objectSystem.out.println("BitSet using set(fromIndex, toIndex) method = " + bitSet);}}
set(int fromIndex, int toIndex, boolean value)
methodThe 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.
public void set(int fromIndex, int toIndex, boolean value)
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.This method does not return anything.
import java.util.BitSet;public class Main{public static void main(String[] args) {// Create empty BitSet objectBitSet bitSet = new BitSet();// Set the bits from index 2 to 5 to value 0bitSet.set(2, 5, false);// Set the bits from index 8 to 10 to value 1bitSet.set(8, 10, true);// Print the BitSet objectSystem.out.println("BitSet using set(fromIndex, toIndex, value) method = " + bitSet);}}