BitSetMath { public static void extractBitRange(BitSet chi, int from, int to, BitSet r) { r.clear(); for (int j = chi.nextSetBit(from); j < to & j != -1; j = chi.nextSetBit(j + 1)) { r.set(j - from); } } private  BitSetMath(); static int increment(BitSet i); static void rotate(BitSet bs, int n, int count); static void extractBitRange(BitSet chi, int from, int to, BitSet r); static void grayCode(BitSet i); static void grayCodeInverse(BitSet i); static void copy(BitSet from, BitSet to); static long littleEndianBitSetToNonNegativeLong(BitSet bs); static void nonNegativeLongToLittleEndianBitSet(long value, BitSet bs);  }
@Test public void extractBitRange() { BitSet iAsBitSet = new BitSet(); BitSet r = new BitSet(); for (int i = 0; i < 64; ++i) { for (int from = 0; from < 32; ++from) { for (int to = from; to < 32; ++to) { BitSet expected = iAsBitSet.get(from, to); BitSetMath.extractBitRange(iAsBitSet, from, to, r); Assert.assertEquals(expected, r); } } BitSetMath.increment(iAsBitSet); } }