diff options
Diffstat (limited to 'power_of_two.c')
-rw-r--r-- | power_of_two.c | 6 |
1 files changed, 1 insertions, 5 deletions
diff --git a/power_of_two.c b/power_of_two.c index 0be3ced..c22b26f 100644 --- a/power_of_two.c +++ b/power_of_two.c @@ -7,11 +7,7 @@ * that 'n == 2^x' */ -bool isPowerOfTwo(int n) { - if (n <= 0) - return 0; - return !(n & (n - 1)); -} +bool isPowerOfTwo(int n) { return n > 0 && __builtin_popcount(n) == 1; } int main() { printf("%d\n", isPowerOfTwo(1)); // expect: 1 |