Skip to content

Instantly share code, notes, and snippets.

@djspiewak
Created July 30, 2018 22:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save djspiewak/c6963a49d428d7113ff6c5ad2b6baa70 to your computer and use it in GitHub Desktop.
Save djspiewak/c6963a49d428d7113ff6c5ad2b6baa70 to your computer and use it in GitHub Desktop.
/**
* Flips all bits for which index % mod == 0 such that
* the bits (i % mod >= 0 && i % mod < mod) are 0. The
* invariant here is that, for all ranges of i such
* that (i % mod >= 0 && i % mod < mod), there is *at least*
* one value of i such that get(i) == true.
*/
public void flipByMod(int mod) {
long flipper = 1L;
long mask = (1L << mod) - 1; // bitmask which covers everything >= 0 and < mod
for (int i = 0; i < _length; i++) {
do {
if ((bits[i] & mask) == 0L) {
bits[i] ^= flipper;
}
mask = Long.rotateLeft(mask, mod);
flipper = Long.rotateLeft(flipper, mod);
} while (!(flipper >= 0 && flipper < mod));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment