Java Utililty Methods Set Cardinality

List of utility methods to do Set Cardinality

Description

The list of methods to do Set Cardinality are organized into topic(s).

Method

intcardinality(byte b)
cardinality
int count = 0;
for (int i = 0; i < 8; ++i) {
    if (((b >>> i) & 1) == 1) {
        ++count;
return count;
intcardinality(final int from, final int to, final long[] words)
cardinality
int sum = 0;
for (int i = from; i < to; i++) {
    sum += Long.bitCount(words[i]);
return sum;
intcardinality(long v)
Compute the cardinality (number of set bits)
return Long.bitCount(v);
intcardinalityInBitmapRange(long[] bitmap, int start, int end)
Hamming weight of the bitset in the range start, start+1,..., end-1
if (start >= end) {
    return 0;
int firstword = start / 64;
int endword = (end - 1) / 64;
if (firstword == endword) {
    return Long.bitCount(bitmap[firstword] & ((~0L << start) & (~0L >>> -end)));
int answer = Long.bitCount(bitmap[firstword] & (~0L << start));
for (int i = firstword + 1; i < endword; i++) {
    answer += Long.bitCount(bitmap[i]);
answer += Long.bitCount(bitmap[endword] & (~0L >>> -end));
return answer;
intcardinalityInBitmapWordRange(long[] bitmap, int start, int end)
Hamming weight of the 64-bit words involved in the range start, start+1,..., end-1
if (start == end) {
    return 0;
int firstword = start / 64;
int endword = (end - 1) / 64;
int answer = 0;
for (int i = firstword; i <= endword; i++) {
    answer += Long.bitCount(bitmap[i]);
...