@Test public void rotateCardinalityTwo() { BitSet bs = new BitSet(); bs.set(5); bs.set(2); BitSet copy = (BitSet) bs.clone(); BitSetMath.rotate(bs, 1000, 0); Assert.assertEquals(copy, bs); BitSetMath.rotate(bs, 5, -5); Assert.assertEquals(copy, bs); BitSet expected = new BitSet(); expected.set(1); expected.set(5); BitSetMath.rotate(bs, 5, 1); Assert.assertEquals(expected, bs); BitSetMath.rotate(bs, 5, -1); Assert.assertEquals(copy, bs); BitSetMath.rotate(bs, 6, -1); expected.clear(); expected.set(0); expected.set(3); Assert.assertEquals(expected, bs); }
public static void rotate(BitSet bs, int n, int count) { int gcd = MathUtils.gcd(n, count); if (gcd != 0) { if (gcd == n) { return; } int m = n / gcd; for (int i = 0; i < gcd; ++i) { boolean previous = bs.get(i); int jTimesCount = 0; for (int j = 1; j <= m; ++j) { jTimesCount += count; int index = i - jTimesCount % n; if (index < 0) { index += n; } boolean current = bs.get(index); if (previous ^ current) { bs.set(index, previous); previous = current; } assert (j < m) ^ (jTimesCount % n == 0); } } } }
BitSetMath { public static void rotate(BitSet bs, int n, int count) { int gcd = MathUtils.gcd(n, count); if (gcd != 0) { if (gcd == n) { return; } int m = n / gcd; for (int i = 0; i < gcd; ++i) { boolean previous = bs.get(i); int jTimesCount = 0; for (int j = 1; j <= m; ++j) { jTimesCount += count; int index = i - jTimesCount % n; if (index < 0) { index += n; } boolean current = bs.get(index); if (previous ^ current) { bs.set(index, previous); previous = current; } assert (j < m) ^ (jTimesCount % n == 0); } } } } }
BitSetMath { public static void rotate(BitSet bs, int n, int count) { int gcd = MathUtils.gcd(n, count); if (gcd != 0) { if (gcd == n) { return; } int m = n / gcd; for (int i = 0; i < gcd; ++i) { boolean previous = bs.get(i); int jTimesCount = 0; for (int j = 1; j <= m; ++j) { jTimesCount += count; int index = i - jTimesCount % n; if (index < 0) { index += n; } boolean current = bs.get(index); if (previous ^ current) { bs.set(index, previous); previous = current; } assert (j < m) ^ (jTimesCount % n == 0); } } } } private  BitSetMath(); }
BitSetMath { public static void rotate(BitSet bs, int n, int count) { int gcd = MathUtils.gcd(n, count); if (gcd != 0) { if (gcd == n) { return; } int m = n / gcd; for (int i = 0; i < gcd; ++i) { boolean previous = bs.get(i); int jTimesCount = 0; for (int j = 1; j <= m; ++j) { jTimesCount += count; int index = i - jTimesCount % n; if (index < 0) { index += n; } boolean current = bs.get(index); if (previous ^ current) { bs.set(index, previous); previous = current; } assert (j < m) ^ (jTimesCount % n == 0); } } } } 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); }
BitSetMath { public static void rotate(BitSet bs, int n, int count) { int gcd = MathUtils.gcd(n, count); if (gcd != 0) { if (gcd == n) { return; } int m = n / gcd; for (int i = 0; i < gcd; ++i) { boolean previous = bs.get(i); int jTimesCount = 0; for (int j = 1; j <= m; ++j) { jTimesCount += count; int index = i - jTimesCount % n; if (index < 0) { index += n; } boolean current = bs.get(index); if (previous ^ current) { bs.set(index, previous); previous = current; } assert (j < m) ^ (jTimesCount % n == 0); } } } } 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);  }