fork(1) download
  1. #include <stdio.h>
  2.  
  3. int isPositive(int x) { return ((~x & (~x + 1)) >> 31) & 1; }
  4.  
  5. int isPowerOfTwo(int x) { return isPositive(x) & ~(x & (x-1)); }
  6.  
  7. int log2(int x)
  8. {
  9. int i = (-1);
  10. while(isPositive(x)) i++, x >>= 1;
  11. return i;
  12. }
  13.  
  14. int isPowerOfNegativeTwo(int x)
  15. {
  16. return ( isPositive(x) & isPowerOfTwo(x) & ~(log2(x) % 2) )
  17. | ( ~isPositive(x) & isPowerOfTwo(-x) & (log2(-x) % 2) );
  18. }
  19.  
  20. int main(void)
  21. {
  22. int x = 256;
  23. printf("%s", isPowerOfNegativeTwo(-512)?"true":"false");
  24. //printf("%s",( isPositive(x) & isPowerOfTwo(x) & ~(log2(x) % 2) )?"true":"false");
  25. return 0;
  26. }
  27.  
Success #stdin #stdout 0s 9432KB
stdin
Standard input is empty
stdout
true