fork download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. template<size_t N, typename T>
  5. size_t bitcount(T v, enable_if_t<(N <= 1)>* = 0)
  6. {
  7. static const unsigned char BitsSetTable256[256] =
  8. {
  9. # define B2(n) n, n+1, n+1, n+2
  10. # define B4(n) B2(n), B2(n+1), B2(n+1), B2(n+2)
  11. # define B6(n) B4(n), B4(n+1), B4(n+1), B4(n+2)
  12. B6(0), B6(1), B6(1), B6(2)
  13. };
  14. return BitsSetTable256[v & 0xff];
  15. }
  16.  
  17. template<size_t N, typename T>
  18. size_t bitcount(T v, enable_if_t<(N > 1)>* = 0)
  19. {
  20. return bitcount<1>(v) + bitcount<N - 1>(v >> 8);
  21. }
  22.  
  23. int main() {
  24. // your code goes here
  25. cout << bitcount<4>(0x01234567) << endl;
  26. cout << bitcount<8>(0x0123456789abcdefULL) << endl;
  27. return 0;
  28. }
Success #stdin #stdout 0s 3412KB
stdin
Standard input is empty
stdout
12
32