Java BitSet cardinality() methodLast Updated : 17 Mar 2025 The cardinality() method of Java BitSet class returns the number of bits set which are true in this BitSet. Syntax:Parameter:NA Returns:The cardinality() method returns the number of bits set which are true in this BitSet. Exception:NA Compatibility Version:Java 1.4 and above Example 1Output:
bitset: {0, 1, 2, 5, 6}
number of true bits: 5
bitset after clear index 2: {0, 1, 5, 6}
number of true bits after clear index 2: 4
Example 2In this example, we are creating a BitSet object using string of bits as input and applying cardinality() method on it. Output:
bitset: {0, 2, 4}
number of true bits: 3
Next TopicJava-bitset-clear-method |
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India