Java Utility Library

Java BitSet - set() Method



The java.util.BitSet.set() method is used to set the bits from the specified fromIndex (inclusive) to the specified toIndex (exclusive) to true in the given BitSet.

Syntax

public void set(int fromIndex, int toIndex)

Parameters

fromIndex Specify the index of the first bit to be set.
toIndex Specify the index after the last bit to be set.

Return Value

void type.

Exception

Throws IndexOutOfBoundsException, if fromIndex is negative, or toIndex is negative, or fromIndex is larger than toIndex.

Example:

In the example below, the java.util.BitSet.set() method is used to assign values in the given BitSet by setting the bit in specified range of 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(0, 10);

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

The output of the above code will be:

BSet contains: {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}

❮ Java.util - BitSet