Power of Two Check
Completed
For each number print yes if it is a power of two using n & (n - 1), else no.
Input
n, then n positive integers.
Output
n lines.
Example
Input
5 1 6 64 100 1024
Output
yes no yes no yes
Editor
C
Hints
A power of two has exactly one bit set; n - 1 flips that bit and everything below it.
Solution explanation
n & (n - 1) clears the lowest set bit - a classic bit trick.
Show solution
#include <stdio.h>
int main() {
int n, x;
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &x);
printf("%s\n", x > 0 && (x & (x - 1)) == 0 ? "yes" : "no");
}
return 0;
}