BufferBitSet { public static int cardinalityTo(@NotNull final Buffer buf, final long bufferOffset, final int bitIndex) { assert bufferOffset <= buf.limit() - (bitIndex / Long.SIZE + 1) * Long.BYTES; int result = 0; final int toWordIndex = bitIndex >> 6; int word = 0; while (word < toWordIndex) { result += Long.bitCount(buf.getLong(bufferOffset + word * Long.BYTES)); word++; } final int bit = bitIndex & 0x3f; final long mask = (1L << bit) - 1; result += Long.bitCount(buf.getLong(bufferOffset + word * Long.BYTES) & mask); return result; }  static boolean get(@NotNull final Buffer buf,
                              final long bufferOffset,
                              final int i); static int nextSetBit(@NotNull final Buffer buf,
                                 final long bufferOffset,
                                 final int sizeHint,
                                 final int fromIndexInclusive); static int cardinalityTo(@NotNull final Buffer buf,
                                    final long bufferOffset,
                                    final int bitIndex); static int arraySize(final int bitCount);  }
@Test public void cardinalityTo() { assertEquals(0, BufferBitSet.cardinalityTo(bitsetBuffer, 0, 0)); assertEquals(1, BufferBitSet.cardinalityTo(bitsetBuffer, 0, 1)); assertEquals(32, BufferBitSet.cardinalityTo(bitsetBuffer, 0, 63)); assertEquals(31, BufferBitSet.cardinalityTo(bitsetBuffer, 0, 62)); assertEquals(33, BufferBitSet.cardinalityTo(bitsetBuffer, 0, 66)); }