aboutsummaryrefslogtreecommitdiff
path: root/power_of_two.c
blob: 0be3ced0e1fe26035bc8f35a5b1d4fc5937a3d3b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// 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) {
  if (n <= 0)
    return 0;
  return !(n & (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
}