forked from lennylxx/leetcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path231.c
27 lines (22 loc) · 747 Bytes
/
231.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
#include <stdio.h>
#include <stdbool.h>
#include <stdint.h>
#include <assert.h>
/**
* Power of 2 have exactly ONE of 1 in the binary, so we don't need a loop.
* We just need to check if it equals to zero after doing ONE time of "n & (n - 1)".
* And doing a logic AND operation to "n > 0" will exclude the negative number and 0.
*/
bool isPowerOfTwo(int n) {
return (n > 0) && !(n & (n - 1));
}
int main () {
assert(isPowerOfTwo(INT32_MIN) == false);
assert(isPowerOfTwo(-1024) == false);
assert(isPowerOfTwo(0) == false);
assert(isPowerOfTwo(1) == true);
assert(isPowerOfTwo(1024) == true);
assert(isPowerOfTwo(INT32_MAX) == false);
printf("all test passed!\n");
return 0;
}