Created
September 30, 2012 09:43
-
-
Save Mortal/3806348 to your computer and use it in GitHub Desktop.
Gauss elimination with XOR
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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