Skip to content
UniCircle
EasyCodingBit manipulation

Count set bits and spot powers of two

Question C-BIT-102

For each unsigned 32-bit number, print how many bits are set to 1, and whether the number is an exact power of two.

Use bitwise operators, not division or pow().

Constraints

  • 1 ≤ n ≤ 100000
  • 0 ≤ each number ≤ 2^32 − 1

Input and output

Input
First line: n. Second line: n numbers, each between 0 and 4294967295.
Output
n lines. Each line has the set-bit count, a space, then `yes` if the number is a power of two (1, 2, 4, 8, …) or `no` otherwise.

Examples

Input 1

4
7 8 0 4294967295

Output

3 no
1 yes
0 no
32 no

7 = 111 (3 bits). 8 = 1000 is a power of two. 0 has no set bits. 2^32 − 1 has all 32 bits set.

Input 2

3
1 6 1024

Output

1 yes
2 no
1 yes

1 = 2^0 and 1024 = 2^10 are powers of two; 6 = 110 is not.

Show hints
  • x & (x - 1) clears the lowest set bit of x.
  • A power of two has exactly one set bit. Is 0 a power of two?
Show the explanation

Kernighan's loop. x & (x - 1) turns off the lowest 1-bit: subtracting 1 flips that bit and every 0 below it, and the AND keeps only the higher bits. Repeat until x is 0 and count the iterations. The loop runs once per set bit, not once per bit position.

Power of two. A power of two has exactly one set bit, so it satisfies x != 0 && (x & (x - 1)) == 0. The x != 0 part matters: 0 has no set bits and is not a power of two.

Types. Use unsigned int (or uint32_t) and read with %u. Values up to 4294967295 do not fit in a signed 32-bit int, and bit tricks on negative signed numbers are a classic source of bugs. GCC and Clang also offer __builtin_popcount, which compiles to a single instruction on most CPUs. Mention it, but show the loop first.

Original UniCircle question. Last reviewed 22 Oct 2026.