Java Utility Library

Java BitSet - length() Method



The java.util.BitSet.length() method returns the "logical size" of this BitSet: the index of the highest set bit in the BitSet plus one. Returns zero if the BitSet contains no set bits.

Syntax

public int length()

Parameters

No parameter is required.

Return Value

Returns the logical size of this BitSet.

Exception

NA

Example:

In the example below, the java.util.BitSet.length() method is used to find out the logical size of the given BitSet.

import java.util.*;

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

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

    System.out.println("Length of BSet: " + BSet.length());    

    //adding one more element in the BitSet
    BSet.set(60); 

    System.out.println("Now, Length of BSet: " + BSet.length());  
  }
}

The output of the above code will be:

Length of BSet: 51
Now, Length of BSet: 61

❮ Java.util - BitSet