BitField implements Comparable<BitField> { public void set(int bit, boolean value) { int bite = byteForBit(bit); ensureSpace(bite + 1); int bitOnByte = bitOnByte(bit, bite); if (value) { bits[bite] = byteBitSet(bitOnByte, bits[bite]); } else { bits[bite] = byteBitUnset(bitOnByte, bits[bite]); } } BitField(); void set(int bit, boolean value); void set(int bit); void unset(int bit); boolean isSet(int bit); void ser(DataOutput out); int deser(DataInput in); int deser(byte[] bytes, int start); @Override int compareTo(BitField o); void clear(); String toString();  }
@Test public void testSet() throws Exception { int MAX = 200; int BITS = 100; Random r = new Random(0); BitField bf = new BitField(); HashSet<Integer> bits = new HashSet<Integer>(); for(int i=0;i<BITS; i++) { int bit = r.nextInt(MAX); bf.set(bit); bits.add(bit); } for(int bit : bits) { assertTrue(bf.isSet(bit)); } for(int i = 0; i<MAX; i++) { if (!bits.contains(i)) { assertFalse(bf.isSet(i)); } } }