HomeLanguagesJavaBitSet length() Method in Java with Examples

BitSet length() Method in Java with Examples

The length() Method of BitSet class in Java is used to know the logical size of this BitSet. This logical size is equal to the highest bit of the BitSet plus one.

Syntax:

BitSet.hashCode()

Parameters: The method does not accept any parameters.

Return Value: The method returns the logical size of the BitSet which is equal to the highest bit of the set plus one. The method returns zero if the BitSet is empty.

Below programs are used to illustrate the working of BitSet.length() Method:
Program 1:




// Java code to illustrate length()
import java.util.*;
  
public class BitSet_Demo {
    public static void main(String args[])
    {
        // Creating an empty BitSet
        BitSet init_bitset = new BitSet();
  
        // Use set() method to add elements into the Set
        init_bitset.set(40);
        init_bitset.set(25);
        init_bitset.set(31);
        init_bitset.set(100);
        init_bitset.set(53);
  
        // Displaying the BitSet
        System.out.println("BitSet: " + init_bitset);
  
        // Displaying the length or logical size
        System.out.println("The Length is: "
                           + init_bitset.length());
    }
}


Output:

BitSet: {25, 31, 40, 53, 100}
The Length is: 101

Program 2:




// Java code to illustrate length()
import java.util.*;
  
public class BitSet_Demo {
    public static void main(String args[])
    {
        // Creating an empty BitSet
        BitSet init_bitset = new BitSet();
  
        // Displaying the BitSet
        System.out.println("BitSet: " + init_bitset);
  
        // Displaying the hashcode
        System.out.println("The Length is: "
                           + init_bitset.length());
    }
}


Output:

BitSet: {}
The Length is: 0
RELATED ARTICLES

3 COMMENTS

Most Popular

Dominic
32544 POSTS0 COMMENTS
Milvus
131 POSTS0 COMMENTS
Nango Kala
6922 POSTS0 COMMENTS
Nicole Veronica
12037 POSTS0 COMMENTS
Nokonwaba Nkukhwana
12143 POSTS0 COMMENTS
Shaida Kate Naidoo
7056 POSTS0 COMMENTS
Ted Musemwa
7294 POSTS0 COMMENTS
Thapelo Manthata
7014 POSTS0 COMMENTS
Umr Jansen
7001 POSTS0 COMMENTS