fork(8) download
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. // returns true if n is power of four
  5. bool checkPowerof4(unsigned n)
  6. {
  7. // return true if n is power of 2 and its only
  8. // set bit is present at even position
  9. return n && !(n & (n - 1)) && !(n & 0xAAAAAAAA);
  10. }
  11.  
  12. int main()
  13. {
  14. unsigned n = 256;
  15.  
  16. if (checkPowerof4(n))
  17. cout << n << " is power of 4";
  18. else
  19. cout << n << " is not a power of 4";
  20.  
  21. return 0;
  22. }
Success #stdin #stdout 0s 3460KB
stdin
Standard input is empty
stdout
256 is power of 4