fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // returns true if n is power of 16
  5. bool checkPowerof16(unsigned n)
  6. {
  7. /*for(int i = 1; i < INT_MAX; i = i*16)
  8. cout << bitset<32>(i) << endl;*/
  9.  
  10. // return true if n is power of 2 and its only
  11. // set bit is present at (0, 4, 8, 12, ...) position
  12. return n && !(n & (n - 1)) && !(n & 0xEEEEEEEE);
  13. }
  14.  
  15. int main()
  16. {
  17. unsigned n = 256*256*16;
  18.  
  19. if (checkPowerof16(n))
  20. cout << n << " is power of 16";
  21. else
  22. cout << n << " is not a power of 16";
  23.  
  24. return 0;
  25. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
1048576 is power of 16