aboutsummaryrefslogtreecommitdiff
path: root/power_of_four.c
blob: 47f88fe8fd0fcd9d51f38fb86d580065c4d7c7fb (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
// 342. Power of Four
#include "leetcode.h"

/*
 * given an integer 'n', return 'true' if it is a power of four, otherwise,
 * return 'false'. an integer 'n' is a power of four if there exists an integer
 * 'x' such that 'n == 4^x'
 */

bool isPowerOfFour(int n) {
  return n > 0 && __builtin_popcount((unsigned)n) == 1 && !((n - 1) % 3);
}

int main() {
  printf("%d\n", isPowerOfFour(16)); // expect: 1
  printf("%d\n", isPowerOfFour(5));  // expect: 0
  printf("%d\n", isPowerOfFour(1));  // expect: 1
}