For each number, print yes if it is a power of two (1, 2, 4, 8, ...) and no otherwise. Use a bitwise test, not a loop.
Input
The first line has q (1 ≤ q ≤ 100). Each of the next q lines has a number from 0 to 2,000,000,000.
Output
yes or no for each number.
Examples
Example 1 · Input
4 1 6 64 0
Expected output
yes no yes no
Example 2 · Input
2 1024 1000
Expected output
yes no
Hints
Hint 1
A power of two has exactly one 1 bit.
Hint 2
n & (n - 1) clears that bit, so for a power of two it gives 0.
Hint 3
Remember that 0 is not a power of two.
Solution
Show one correct solution (try it yourself first)
#include <stdio.h>
int main(void) {
int q;
scanf("%d", &q);
for (int i = 0; i < q; i++) {
int n;
scanf("%d", &n);
printf("%s\n", n > 0 && (n & (n - 1)) == 0 ? "yes" : "no");
}
return 0;
}