fork download
  1. // author Masaki Hara (qnighy)
  2. // Public Domain
  3. #include <cstdio>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. // for 64-bit environment, but works on 32bit too
  8. template<size_t N> struct roundUp {
  9. // corner case: x=0, x>2^63
  10. static const size_t _N1 = N-1;
  11. static const size_t _N2 = _N1 | (_N1 >> 1);
  12. static const size_t _N3 = _N2 | (_N2 >> 2);
  13. static const size_t _N4 = _N3 | (_N3 >> 4);
  14. static const size_t _N5 = _N4 | (_N4 >> 8);
  15. static const size_t _N6 = _N5 | (_N5 >> 16);
  16. static const size_t _N7 = _N6 | (_N6 >> 32);
  17. static const size_t Value = _N7 + 1;
  18. };
  19.  
  20. int main() {
  21. cout << roundUp< 1>::Value << endl;
  22. cout << roundUp< 2>::Value << endl;
  23. cout << roundUp< 3>::Value << endl;
  24. cout << roundUp< 4>::Value << endl;
  25. cout << roundUp< 5>::Value << endl;
  26. cout << roundUp< 6>::Value << endl;
  27. cout << roundUp< 7>::Value << endl;
  28. cout << roundUp< 8>::Value << endl;
  29. cout << roundUp< 9>::Value << endl;
  30. cout << roundUp<10>::Value << endl;
  31. return 0;
  32. }
  33.  
Success #stdin #stdout 0.01s 2724KB
stdin
Standard input is empty
stdout
1
2
4
4
8
8
8
8
16
16