Any quick method to count the number of set bits in a BitSet other than the usual 'keep a counter' method?
Bitset internally uses long (64 bits).
bitset::count() is an inbuilt STL in C++ which returns the number of set bits in the binary representation of a number. Parameter: The function accepts no parameter. Return Value: The function returns the number of set bits.
BitSet is a class defined in the java. util package. It creates an array of bits represented by boolean values. The size of the array is flexible and can grow to accommodate additional bit as needed.
The cardinality() method returns the number of set bits.
(Assuming you don't want to call cardinality())
int count = 0;
for (int i = bs.nextSetBit(0); i >= 0; i = bs.nextSetBit(i+1)) {
count++;
}
see javadoc
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With