Skip to content

Instantly share code, notes, and snippets.

@Mortal
Created September 30, 2012 09:43
Show Gist options
  • Save Mortal/3806348 to your computer and use it in GitHub Desktop.
Save Mortal/3806348 to your computer and use it in GitHub Desktop.
Gauss elimination with XOR
void gauss(vector<size_t> & nums) {
for (size_t bit = 63; bit--;) {
size_t findmask = 1ull << bit;
size_t maxmask = 2*findmask-1;
// search for number with given highest bit,
// i.e. a number with `findmask` not greater than `maxmask`
size_t j = 0;
while (j < nums.size()) {
if (nums[j] & findmask && nums[j] <= maxmask) break;
++j;
}
if (j == nums.size()) continue;
// eliminate bit in all entries but one
for (size_t i = 0; i < nums.size(); ++i) {
if (i == j) continue;
if (nums[i] & findmask) nums[i] ^= nums[j];
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment