CustomBitSet implements Closeable { public void setBit(final long bitOffset) { if (bitOffset < 0) { throw new IllegalArgumentException("Negative bitOffset value " + bitOffset); } final int byteToSet = (int)(bitOffset / 8); if (byteToSet > nrBytes) { throw new IllegalArgumentException("bitOffset value " + bitOffset + " (byte offset " + byteToSet + ") exceeds buffer capacity of " + nrBytes + " bytes"); } byte b = buf.get(byteToSet); byte posBit = (byte) (1 << (7 - (bitOffset % 8))); b |= posBit; buf.put(byteToSet, b); } private  CustomBitSet(final int nrBytes); private  CustomBitSet(final int nrBytes, final ByteBuffer buffer); static CustomBitSet withByteBuffer(final int numBytes, ByteBuffer byteBuffer); static CustomBitSet withByteLength(final int nrBytes); static CustomBitSet withBitLength(final int nrBits); void setBit(final long bitOffset); void unsetBit(final long bitOffset); int readInt(long startBitIndex, long endBitIndex); byte[] toByteArray(); String toString(); long findNthBitSetAfter(long startBitIndex, int n); long nextSetBit(long index); long nextSetBitAfter(long index); boolean isBitSet(long index); @Override void close();  }
@Test public void testSetBit() { final int LENGTH = 256; CustomBitSet customBitSet = CustomBitSet.withBitLength(LENGTH); for (int i = 0; i < LENGTH; i++) { if (i % 2 == 0) { customBitSet.setBit(i); } } for (int i = 0; i < LENGTH; i++) { if (i % 2 == 0) { Assert.assertTrue(customBitSet.isBitSet(i)); } else { Assert.assertFalse(customBitSet.isBitSet(i)); } } customBitSet.close(); }