Returns the number of bits set to true in the bitset. - Java java.util

Java examples for java.util:BitSet

Description

Returns the number of bits set to true in the bitset.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        int bitset = 2;
        System.out.println(cardinality(bitset));
    }//  www . j a  v a 2  s .co m

    /**
     * Returns the number of bits set to <tt>true</tt> in the bitset.
     * 
     * @param  bitset
     *         the bitset.
     * @return the number of bits set to <tt>true</tt> in the bitset.
     */
    public static int cardinality(int bitset) {
        return Integer.bitCount(bitset);
    }
}

Related Tutorials