BitSetMath { public static void grayCode(BitSet i) { boolean oneBeyond = true; for (int j = i.length() - 2; j >= 0; --j) { boolean current = i.get(j); if (oneBeyond) { i.set(j, !current); } oneBeyond = current; } } 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 grayCode() { BitSet bs = new BitSet(); BitSetMath.grayCode(bs); Assert.assertTrue(bs.isEmpty()); bs.set(0); BitSet expected = (BitSet) bs.clone(); BitSetMath.grayCode(bs); Assert.assertEquals(expected, bs); bs.set(1); expected.clear(); expected.set(1); BitSetMath.grayCode(bs); Assert.assertEquals(expected, bs); expected.clear(); expected.set(0); expected.set(1); BitSetMath.grayCode(bs); Assert.assertEquals(expected, bs); }