Java BitSet cardinality() Method



Description

The Java BitSet cardinality() method returns the number of bits set to true in this BitSet.

Declaration

Following is the declaration for java.util.BitSet.cardinality() method

public int cardinality()

Parameters

NA

Return Value

This method returns the number of bits set to true in this BitSet.

Exception

NA

Example 1

The following example shows the usage of Java BitSet cardinality() method. We're creating two BitSets. We're setting true values at given indexes to the BitSet objects using set() method calls per index and using cardinality() method we're printing the number of bits which are true in these bitsets.

package com.tutorialspoint;
import java.util.BitSet;
public class BitSetDemo {
   public static void main(String[] args) {

      // create 2 bitsets
      BitSet bitset1 = new BitSet();
      BitSet bitset2 = new BitSet();

      // assign values to bitset1
      bitset1.set(0, 6, true);

      // assign values to bitset2
      bitset2.set(2);
      bitset2.set(4);
      bitset2.set(6);
      bitset2.set(8);
      bitset2.set(10);

      // print the sets
      System.out.println("Bitset1:" + bitset1);
      System.out.println("Bitset2:" + bitset2);

      // print cardinality for bitset1
      System.out.println(bitset1.cardinality());

      // print cardinality for bitset2
      System.out.println(bitset2.cardinality());
   }
}

Let us compile and run the above program, this will produce the following result −

Bitset1:{0, 1, 2, 3, 4, 5}
Bitset2:{2, 4, 6, 8, 10}
{2, 4}

Example 2

The following example shows the usage of Java BitSet and() method. We're creating two BitSets using byte[] and using cardinality() method we're printing the number of bits which are true in these bitsets.

package com.tutorialspoint;
import java.util.BitSet;
public class BitSetDemo {
   public static void main(String[] args) {

      // create 2 bitsets
      BitSet bitset1 = BitSet.valueOf(new byte[] { 0, 1, 2, 3, 4, 5 });
      BitSet bitset2 = BitSet.valueOf(new byte[] { 2, 4, 6, 8, 10 });

      // print the sets
      System.out.println("Bitset1:" + bitset1);
      System.out.println("Bitset2:" + bitset2);

      // print cardinality for bitset1
      System.out.println(bitset1.cardinality());

      // print cardinality for bitset2
      System.out.println(bitset2.cardinality());
   }
}

Let us compile and run the above program, this will produce the following result −

Bitset1:{8, 17, 24, 25, 34, 40, 42}
Bitset2:{1, 10, 17, 18, 27, 33, 35}
7
7

Example 3

The following example shows the usage of Java BitSet and() method. We're creating two BitSets using long[] and using cardinality() method we're printing the number of bits which are true in these bitsets.

package com.tutorialspoint;
import java.util.BitSet;
public class BitSetDemo {
   public static void main(String[] args) {

      // create 2 bitsets
      BitSet bitset1 = BitSet.valueOf(new long[] { 0, 1, 2, 3, 4, 5 });
      BitSet bitset2 = BitSet.valueOf(new long[] { 2, 4, 6, 8, 10 });

      // print the sets
      System.out.println("Bitset1:" + bitset1);
      System.out.println("Bitset2:" + bitset2);

      // print cardinality for bitset1
      System.out.println(bitset1.cardinality());

      // print cardinality for bitset2
      System.out.println(bitset2.cardinality());
   }
}

Let us compile and run the above program, this will produce the following result −

Bitset1:{64, 129, 192, 193, 258, 320, 322}
Bitset2:{1, 66, 129, 130, 195, 257, 259}
7
7
java_util_bitset.htm
Advertisements