Given a positive integer, write a function to find if it is a power of two or not.

**Examples :**

Input : n = 4 Output : Yes 2^{2}= 4 Input : n = 7 Output : No Input : n = 32 Output : Yes 2^{5}= 32

**1. **A simple method for this is to simply take the log of the number on base 2 and if you get an integer then number is power of 2.

## Java

`// Java Program to find whether a ` `// no is power of two ` `class` `GFG { ` ` ` `/* Function to check if x is power of 2*/` ` ` `static` `boolean` `isPowerOfTwo(` `int` `n) ` ` ` `{ ` ` ` `return` `(` `int` `)(Math.ceil((Math.log(n) / Math.log(` `2` `)))) ` ` ` `== (` `int` `)(Math.floor(((Math.log(n) / Math.log(` `2` `))))); ` ` ` `} ` ` ` ` ` `// Driver Code ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `if` `(isPowerOfTwo(` `31` `)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` ` ` `if` `(isPowerOfTwo(` `64` `)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by mits ` |

*chevron_right*

*filter_none*

**Output:**

No Yes

**2. **Another solution is to keep dividing the number by two, i.e, do n = n/2 iteratively. In any iteration, if n%2 becomes non-zero and n is not 1 then n is not a power of 2. If n becomes 1 then it is a power of 2.

## Java

`// Java program to find whether ` `// a no is power of two ` `import` `java.io.*; ` ` ` `class` `GFG { ` ` ` ` ` `// Function to check if ` ` ` `// x is power of 2 ` ` ` `static` `boolean` `isPowerOfTwo(` `int` `n) ` ` ` `{ ` ` ` `if` `(n == ` `0` `) ` ` ` `return` `false` `; ` ` ` ` ` `while` `(n != ` `1` `) { ` ` ` `if` `(n % ` `2` `!= ` `0` `) ` ` ` `return` `false` `; ` ` ` `n = n / ` `2` `; ` ` ` `} ` ` ` `return` `true` `; ` ` ` `} ` ` ` ` ` `// Driver program ` ` ` `public` `static` `void` `main(String args[]) ` ` ` `{ ` ` ` `if` `(isPowerOfTwo(` `31` `)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` ` ` `if` `(isPowerOfTwo(` `64` `)) ` ` ` `System.out.println(` `"Yes"` `); ` ` ` `else` ` ` `System.out.println(` `"No"` `); ` ` ` `} ` `} ` ` ` `// This code is contributed by Nikita tiwari. ` |

*chevron_right*

*filter_none*

**Output:**

No Yes

**3. **All power of two numbers have only one bit set. So count the no. of set bits and if you get 1 then number is a power of 2. Please see Count set bits in an integer for counting set bits.

**4. ** 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 become unset.

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

3 –> 011

15 –> 01111

So, if a number n is a 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 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)) (thanks to https://www.geeksforgeeks.org/program-to-find-whether-a-no-is-power-of-two/Mohammad for adding this case).

Below is the implementation of this method.

## Java

`// Java program to efficiently ` `// check for power for 2 ` ` ` `class` `Test { ` ` ` `/* Method to check if x is power of 2*/` ` ` `static` `boolean` `isPowerOfTwo(` `int` `x) ` ` ` `{ ` ` ` `/* First x in the below expression is ` ` ` `for the case when x is 0 */` ` ` `return` `x != ` `0` `&& ((x & (x - ` `1` `)) == ` `0` `); ` ` ` `} ` ` ` ` ` `// Driver method ` ` ` `public` `static` `void` `main(String[] args) ` ` ` `{ ` ` ` `System.out.println(isPowerOfTwo(` `31` `) ? ` `"Yes"` `: ` `"No"` `); ` ` ` `System.out.println(isPowerOfTwo(` `64` `) ? ` `"Yes"` `: ` `"No"` `); ` ` ` `} ` `} ` `// This program is contributed by Gaurav Miglani ` |

*chevron_right*

*filter_none*

**Output:**

No Yes

Please refer complete article on Program to find whether a no is power of two for more details!

## Recommended Posts:

- Java Program to check whether it is possible to make a divisible by 3 number using all digits in an array
- Check whether the number formed by concatenating two numbers is a perfect square or not
- Java Program to Find the closest pair from two sorted arrays
- Java Program for Program to find area of a circle
- Java Program for GCD of more than two (or array) numbers
- Java Program for Common Divisors of Two Numbers
- Java Program to Multiply two Matrices of any size
- Java Program to Add two Matrices
- Java Program to Swap two Variables
- Java Program for Program to calculate area of a Tetrahedron
- Java Program for Program for array rotation
- Find the duration of difference between two dates in Java
- Java Program to find the sum of a Series 1/1! + 2/2! + 3/3! + 4/4! +.......+ n/n!
- Java Program to Find remainder of array multiplication divided by n
- Java Program for Find the perimeter of a cylinder
- Java Program to Find sum of even factors of a number
- Java Program to Find minimum sum of factors of number
- Java Program for Find largest prime factor of a number
- Java Program to find largest element in an array
- Java Program to find sum of array