blob: c22b26f24975656e1b83b88509765807ed6dbfc8 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
// 231. Power of Two
#include "leetcode.h"
/*
* given an integer 'n', return true if it is a power of two. otherwise, return
* false. an integer 'n' is a power of two if there exists an integer 'x' such
* that 'n == 2^x'
*/
bool isPowerOfTwo(int n) { return n > 0 && __builtin_popcount(n) == 1; }
int main() {
printf("%d\n", isPowerOfTwo(1)); // expect: 1
printf("%d\n", isPowerOfTwo(16)); // expect: 1
printf("%d\n", isPowerOfTwo(3)); // expect: 0
}
|