Guest User

Untitled

a guest
Feb 20th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.35 KB | None | 0 0
  1. /**
  2. * check_pow2: Returns 1 if n is power of 2
  3. */
  4. #include <stdio.h>
  5. #include <assert.h>
  6.  
  7. static int check_pow2(unsigned int n) {
  8. return n != 0 && (n&n-1) == 0;
  9. }
  10.  
  11. int main(void) {
  12. assert(!check_pow2(0));
  13. assert(check_pow2(1));
  14. assert(check_pow2(2));
  15. assert(check_pow2(1<<16));
  16. assert(!check_pow2((1<<16)+1));
  17. return 0;
  18. }
Add Comment
Please, Sign In to add comment