New update is available. Click here to update.

Last Updated: 22 Nov, 2020

Difficulty: Easy

```
The first line of input contains an integer ‘T’ representing the number of test cases. Then the test cases follow.
The only line of each test case contains an integer 'N'.
```

```
For each test case, the only line of output prints whether the integer ‘N’ is a power of 2 or not.
The output for each test case is in a separate line.
```

```
You do not need to print anything; it has already been taken care of. Just implement the given function.
```

```
1 <= T <= 100
-2^31 <= N <= 2^31 - 1
Time Limit: 1sec
```

The basic idea is to use Bit Manipulation. If we subtract a power of 2 numbers by 1 then all unset bits after the only set bit become set; and the set bit becomes unset.

For example for 4 (100) and 16 (10000), we get the following after subtracting 1

3 -> 011

15 -> 01111

So, if a number ‘N’ is power of 2 then bitwise & of ‘N’ and ‘N’-1 will be zero. We can say ‘N’ is a power of 2 or not based on the value of ‘N’&('N'-1). The expression ‘N’&('N'-1) will not work when ‘N’ is 0. To handle this case also, our expression will become ‘N’&(!'N'&('N'-1)).

SIMILAR PROBLEMS

Check whether K-th bit is set or not

Posted: 20 May, 2022

Difficulty: Easy

Check whether K-th bit is set or not

Posted: 20 May, 2022

Difficulty: Easy

Maximum Element

Posted: 21 Jul, 2022

Difficulty: Moderate

XOR DARE

Posted: 3 Sep, 2022

Difficulty: Moderate

Merge Two Sorted Arrays Without Extra Space

Posted: 19 Nov, 2022

Difficulty: Moderate

Co-Prime

Posted: 14 Dec, 2022

Difficulty: Hard

Popular Interview Problems: