fork(2) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // returns true if n is power of 8
  5. bool checkPowerof8(unsigned n)
  6. {
  7. /* for(int i = 1; i < INT_MAX; i = i*8)
  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, 3, 6, ... ) position
  12. return n && !(n & (n - 1)) && !(n & 0xB6DB6DB6);
  13. }
  14.  
  15. int main()
  16. {
  17. unsigned n = 512;
  18.  
  19. if (checkPowerof8(n))
  20. cout << n << " is power of 8";
  21. else
  22. cout << n << " is not a power of 8";
  23.  
  24. return 0;
  25. }
Success #stdin #stdout 0s 3468KB
stdin
Standard input is empty
stdout
512 is power of 8