Trusted answers to developer questions
Trusted Answers to Developer Questions

Related Tags

java
communitycreator

What is BitSet.cardinality() in Java?

abhilash

cardinality() is an instance method of the BitSet class that is used to get the number of bits set to True.

The cardinality method is defined in the BitSet class. The BitSet class is defined in the java.util package. To import the BitSet class, use the following import statement.

import java.util.BitSet;

Syntax


public int cardinality()

Parameters

This method has no parameters.

Return value

This method returns the number of set bits.

Code

import java.util.BitSet;

public class Main{

    public static void main(String[] args) {
        // Create empty BitSet object
        BitSet bitSet = new BitSet();

        // Set the bit at index 2
        bitSet.set(3);

        // Get the number of set bits using cardinality method
        System.out.printf("%s.cardinality() = %s" , bitSet, bitSet.cardinality());
    }
}
Calling the bitSet.cardinality() method

RELATED TAGS

java
communitycreator
RELATED COURSES

View all Courses

Keep Exploring