Java Utility Library

Java BitSet - get() Method



The java.util.BitSet.get() method returns the value of the bit with the specified index. The method returns true if the bit with the index bitIndex is currently set in this BitSet, otherwise returns false.

Syntax

public boolean get(int bitIndex)

Parameters

bitIndex Specify the bit index.

Return Value

Returns the value of the bit with the specified index.

Exception

Throws IndexOutOfBoundsException, if the specified index is negative.

Example:

In the example below, the java.util.BitSet.get() method is used to check the bit value with the specified index is present in the given BitSet or not.

import java.util.*;

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

    //populating BitSet
    BSet.set(2);
    BSet.set(4);
    BSet.set(6);
    BSet.set(8);
    BSet.set(10);

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

    //check the presence of bit value with
    //specified index in the given BitSet
    System.out.println(BSet.get(5));
    System.out.println(BSet.get(6));
  }
}

The output of the above code will be:

BSet contains: {2, 4, 6, 8, 10}
false
true

❮ Java.util - BitSet