BufferBitSet { public static int nextSetBit(@NotNull final Buffer buf, final long bufferOffset, final int sizeHint, final int fromIndexInclusive) { assert fromIndexInclusive >= 0; assert bufferOffset <= buf.limit() - (fromIndexInclusive / Long.SIZE + 1) * Long.BYTES; if (fromIndexInclusive >= sizeHint) { return -1; } final int wordCount = arraySize(sizeHint); int u = fromIndexInclusive >> 6; long word = buf.getLong(bufferOffset + u * Long.BYTES); if (word == ~0L) return fromIndexInclusive; word &= 0xffffffffffffffffL << fromIndexInclusive; while (true) { if (word != 0) { return (u << 6) | Long.numberOfTrailingZeros(word); } if (++u == wordCount) { return -1; } word = buf.getLong(bufferOffset + u * Long.BYTES); } }  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 nextSetBit() { assertEquals(0, BufferBitSet.nextSetBit(bitsetBuffer, 0, 128, 0)); assertEquals(2, BufferBitSet.nextSetBit(bitsetBuffer, 0, 128, 1)); assertEquals(65, BufferBitSet.nextSetBit(bitsetBuffer, 0, 128, 64)); assertEquals(127, BufferBitSet.nextSetBit(bitsetBuffer, 0, 128, 126)); assertEquals(160, BufferBitSet.nextSetBit(bitsetBuffer, 0, 192, 160)); assertEquals(256, BufferBitSet.nextSetBit(bitsetBuffer, 0, 320, 200)); assertEquals(-1, BufferBitSet.nextSetBit(bitsetBuffer, 0, 192, 193)); }
@Test public void nextSetBitOne() { assertEquals(0, BufferBitSet.nextSetBit(oneBuffer, 0, 128, 0)); }
@Test(expected = AssertionError.class) public void nextSetBitError() { assertEquals(-1, BufferBitSet.nextSetBit(bitsetBuffer, 0, 128, 512)); }
@Test public void nextSetBitNotFound() { assertEquals(-1, BufferBitSet.nextSetBit(emptyBuffer, 0, 64, 0)); }