Guest User

Untitled

a guest
Feb 21st, 2018
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.95 KB | None | 0 0
  1. #include <iostream>
  2. #include <cassert>
  3. using namespace std;
  4.  
  5. #define N 16 // N must be either 16 or 32
  6.  
  7. #if N==16
  8. typedef int16_t Int;
  9. #else
  10. typedef int32_t Int;
  11. #endif
  12.  
  13. int bitsum(Int n){
  14. // Count the one bits in n
  15. int count = 0;
  16. while (n != 0){
  17. count += 1;
  18. n &= n-1;
  19. }
  20. return count;
  21. }
  22.  
  23. int main() {
  24. assert(N==16 or N==32);
  25. #if N==16
  26. Int w[5];
  27. w[0] = 0;
  28. w[1] = 0x5555;
  29. w[2] = 0x3333;
  30. w[3] = 0x0f0f;
  31. w[4] = 0x00ff;
  32. #else
  33. Int w[6];
  34. w[0] = 0;
  35. w[1] = 0x55555555;
  36. w[2] = 0x33333333;
  37. w[3] = 0x0f0f0f0f;
  38. w[4] = 0x00ff00ff;
  39. w[5] = 0x0000ffff;
  40. #endif
  41. int count = 1; // we know v = 0 works
  42. for (Int v = 1; v != 0; ++v) {
  43. if (bitsum(v)%2 != 0) continue;
  44. for (auto x:w) {
  45. if (bitsum(v^x)== N/2) {
  46. ++count;
  47. break;
  48. }
  49. }
  50. }
  51. cout << "count = " <<count << " for n = " << N << endl;
  52. return 0;
  53. }
Add Comment
Please, Sign In to add comment