Write a method - **O(1)** time and use **O(1)** space.

Examples:

`isPowOfTwo`

to test whether or not a given positive integer is a power of 2. Your method should run in constant Examples:

`isPowOfTwo(5) ==> false`

`isPowOfTwo(8) ==> true`

Need a **hand?** Try out these hints, one at a time.

Since this solution demands a constant runtime, loops are ruled out! This means that bit manipulation is the way to go.

This is clearly a trick question - so don't feel upset if the answer isn't all that obvious. The trick here is that if a number **bitwise** & of it with

`n`

is a power of two, the `n-1`

will always return 0. Let's take a look at `4`

in binary. `4`

= `100`

. Now, `4-1=3`

, and `3=011`

in binary. The bitwise & of `100 & 011 = 000`

public static boolean isPowOfTwo(int num) { }

**C**

**Java**

**Python**