Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cassert>
- using namespace std;
- #define N 16 // N must be either 16 or 32
- #if N==16
- typedef int16_t Int;
- #else
- typedef int32_t Int;
- #endif
- int bitsum(Int n){
- // Count the one bits in n
- int count = 0;
- while (n != 0){
- count += 1;
- n &= n-1;
- }
- return count;
- }
- int main() {
- assert(N==16 or N==32);
- #if N==16
- Int w[5];
- w[0] = 0;
- w[1] = 0x5555;
- w[2] = 0x3333;
- w[3] = 0x0f0f;
- w[4] = 0x00ff;
- #else
- Int w[6];
- w[0] = 0;
- w[1] = 0x55555555;
- w[2] = 0x33333333;
- w[3] = 0x0f0f0f0f;
- w[4] = 0x00ff00ff;
- w[5] = 0x0000ffff;
- #endif
- int count = 1; // we know v = 0 works
- for (Int v = 1; v != 0; ++v) {
- if (bitsum(v)%2 != 0) continue;
- for (auto x:w) {
- if (bitsum(v^x)== N/2) {
- ++count;
- break;
- }
- }
- }
- cout << "count = " <<count << " for n = " << N << endl;
- return 0;
- }
Add Comment
Please, Sign In to add comment