Skills101
← All coding problems

Power of Two

easyBit Manipulation+10 XPTime limit 2s

For each integer, decide whether it is a power of two (1, 2, 4, 8, …).

Input:

  • line 1: q
  • next q lines: one integer x each

Output: for every x, true or false on its own line.

Constraints: 1 ≤ q ≤ 10⁵, −2³¹ ≤ x ≤ 2³¹ − 1.

One-liner: a power of two has exactly one 1 bit, so x > 0 && (x & (x − 1)) == 0. Don't forget that 0 and negative numbers are not powers of two.

Sample input 1

3
1
16
3

Sample output 1

true
true
false

Sample input 2

3
0
-16
1073741824

Sample output 2

false
false
true

📖 Stuck? Learn the pattern

Read input from standard input and print the answer to standard output. Your code runs in a secure sandbox against the samples (Run) and hidden tests (Submit).

Loading editor…

Log in to run and submit your code — it's free.