Java Utility Library

Java BitSet - set() Method



The java.util.BitSet.set() method is used to set the bit at the specified index to true in the given BitSet.

Syntax

public void set(int bitIndex)

Parameters

bitIndex Specify a bit index.

Return Value

void type.

Exception

Throws IndexOutOfBoundsException, if the specified index is negative.

Example:

In the example below, the java.util.BitSet.set() method is used to assign values in the given BitSet by setting the bit at the specified index to true.

import java.util.*;

public class MyClass {
  public static void main(String[] args) {   
    //creating a BitSet
    BitSet BSet = new BitSet();

    //populating BitSet using set method
    BSet.set(10);
    BSet.set(20);
    BSet.set(30);
    BSet.set(40);
    BSet.set(50);

    //printing BitSet
    System.out.print("BSet contains: " + BSet);
  }
}

The output of the above code will be:

BSet contains: {10, 20, 30, 40, 50}

❮ Java.util - BitSet