fork(7) download
  1. #include <cstdint>
  2. #include <iomanip>
  3. #include <iostream>
  4.  
  5. uint64_t calc(uint64_t n)
  6. {
  7. // (odd | even)
  8. uint64_t x = (n & 0x5555555555555555ull) | ((n & 0xAAAAAAAAAAAAAAAAull) >> 1);
  9.  
  10. // deinterleave
  11. x = (x | (x >> 1)) & 0x3333333333333333ull;
  12. x = (x | (x >> 2)) & 0x0F0F0F0F0F0F0F0Full;
  13. x = (x | (x >> 4)) & 0x00FF00FF00FF00FFull;
  14. x = (x | (x >> 8)) & 0x0000FFFF0000FFFFull;
  15. x = (x | (x >> 16)) & 0x00000000FFFFFFFFull;
  16.  
  17. return x;
  18. }
  19.  
  20. int main()
  21. {
  22. uint64_t n;
  23. while (std::cin >> n) {
  24. std::cout << std::hex << n << " -> " << calc(n) << '\n';
  25. }
  26. }
  27.  
Success #stdin #stdout 0s 3464KB
stdin
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
1431655765
2694860880
2863311530
4294901760
stdout
0 -> 0
1 -> 1
2 -> 1
3 -> 1
4 -> 2
5 -> 3
6 -> 3
7 -> 3
8 -> 2
9 -> 3
a -> 3
b -> 3
c -> 2
d -> 3
e -> 3
f -> 3
55555555 -> ffff
a0a05050 -> cccc
aaaaaaaa -> ffff
ffff0000 -> ff00