Under construction

Still being built. Things may change

Yet Another No-Solve Prevention Problem!!

Interview

Time limit2sMemory limit512 MB

Summary
For each of Q queries, decide whether the given number a is a power of two, printing 1 for yes and 0 for no.
Level

Easy3 of 10

Topics
Bit manipulation, Math, Implementation, Brute force
Solved
No attempts yet

Problem

You must process QQ queries. Each query is the following.

Can the number aa be written as a power of two?

Input

The first line gives QQ. (1≤Q≤1061 \le Q \le 10^6)

The second line through the (Q+1)(Q+1)-th line give aa. Each aa is a natural number with 1≤a≤231−11 \le a \le 2^{31}-1.

Output

For each query, print 1 if the answer is Yes, and 0 otherwise.

Hint

Suppose some number aa can be written as a power of two. Then a=2na = 2^n for some integer n≥0n \ge 0. Usually, checking each bit and counting the number of set bits is also a good approach. This performs at most about 32 operations, so with QQ queries in total the time complexity is O(32Q)O(32Q).

But is there a better way? Write xx (x≥0x \ge 0) in binary, and split into the cases where xx is odd and where xx is even.

...01

How is −x-x written then?

...11

So x&(−x)x\&(-x) gives 1. What if xx is even?

...10...0

This is how it is written. How is −x-x written then? First flip the bits.

...01...1

Adding 1 to this is the two's complement representation. So −x-x is written as

...10...0

Therefore x&(−x)x\&(-x) is related to the lowest set bit of xx. Here is an example. When x=6x=6, what is the value of x&(−x)x\&(-x)?

6=...01106 = ...0110

−6=...1010-6 = ...1010

so 6&(−6)=26\&(-6) = 2.

Now observe what property aa has when it is written as 2i2^i.

a=0...010...0a = 0...010...0

−a=1...110...0-a = 1...110...0

So if (a&(−a))==a(a\&(-a)) == a, then aa is a power of two; otherwise it is not.

Examples1

  1. Example 1

    Input
    10
    1
    2
    7
    4
    14
    32
    33
    34
    35
    36
    
    Expected output
    1
    1
    0
    1
    0
    1
    0
    0
    0
    0