# Program to find whether a given number is power of 2

• Difficulty Level : Easy
• Last Updated : 18 Nov, 2022

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

Examples:

Input : n = 4
Output : Yes
Explanation: 22 = 4

Input : n = 32
Output : Yes
Explanation: 25 = 32

Recommended Practice

To solve the problem follow the below idea:

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

Below is the implementation of the above approach:

## C++

 // C++ Program to find whether a // no is a power of two #include using namespace std;   // Function to check if x is power of 2 bool isPowerOfTwo(int n) {     if (n == 0)         return false;       return (ceil(log2(n)) == floor(log2(n))); }   // Driver code int main() {     // Function call     isPowerOfTwo(31) ? cout << "Yes" << endl                      : cout << "No" << endl;     isPowerOfTwo(64) ? cout << "Yes" << endl                      : cout << "No" << endl;       return 0; }   // This code is contributed by Surendra_Gangwar

## C

 // C Program to find whether a // no is power of two #include #include #include   /* Function to check if x is power of 2*/ bool isPowerOfTwo(int n) {     if (n == 0)         return false;       return (ceil(log2(n)) == floor(log2(n))); }   // Driver code int main() {     // Function call     isPowerOfTwo(31) ? printf("Yes\n") : printf("No\n");     isPowerOfTwo(64) ? printf("Yes\n") : printf("No\n");     return 0; }   // This code is contributed by bibhudhendra

## Java

 // Java Program to find whether a // no is power of two import java.lang.Math;   class GFG {     /* Function to check if x is power of 2*/     static boolean isPowerOfTwo(int n)     {         if (n == 0)             return false;           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)     {         // Function call         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

## Python3

 # Python3 Program to find # whether a no is # power of two import math   # Function to check # Log base 2     def Log2(x):     if x == 0:         return false       return (math.log10(x) /             math.log10(2))   # Function to check # if x is power of 2     def isPowerOfTwo(n):     return (math.ceil(Log2(n)) ==             math.floor(Log2(n)))     # Driver Code if __name__ == "__main__":       # Function call     if(isPowerOfTwo(31)):         print("Yes")     else:         print("No")       if(isPowerOfTwo(64)):         print("Yes")     else:         print("No")   # This code is contributed # by mits

## C#

 // C# Program to find whether // a no is power of two using System;   class GFG {       /* Function to check if        x is power of 2*/     static bool isPowerOfTwo(int n)     {           if (n == 0)             return false;           return (int)(Math.Ceiling(                    (Math.Log(n) / Math.Log(2))))             == (int)(Math.Floor(                 ((Math.Log(n) / Math.Log(2)))));     }       // Driver Code     public static void Main()     {           // Function call         if (isPowerOfTwo(31))             Console.WriteLine("Yes");         else             Console.WriteLine("No");           if (isPowerOfTwo(64))             Console.WriteLine("Yes");         else             Console.WriteLine("No");     } }   // This code is contributed // by Akanksha Rai(Abby_akku)



## Javascript



Output

No
Yes

Time Complexity: O(1)
Auxiliary Space: O(1)

## Find whether a given number is a power of 2 using the division operator:

To solve the problem follow the below idea:

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.

Below is the implementation of the above approach:

## C++

 // C++ program for the above approach   #include using namespace std;   /* Function to check if x is power of 2*/ bool isPowerOfTwo(int n) {     if (n == 0)         return 0;     while (n != 1) {         if (n % 2 != 0)             return 0;         n = n / 2;     }     return 1; }   // Driver code int main() {     // Function call     isPowerOfTwo(31) ? cout << "Yes\n" : cout << "No\n";     isPowerOfTwo(64) ? cout << "Yes\n" : cout << "No\n";     return 0; }   // This code is contributed by rathbhupendra

## C

 // C program for the above approach   #include #include   /* Function to check if x is power of 2*/ bool isPowerOfTwo(int n) {     if (n == 0)         return 0;     while (n != 1) {         if (n % 2 != 0)             return 0;         n = n / 2;     }     return 1; }   // Driver code int main() {     // Function call     isPowerOfTwo(31) ? printf("Yes\n") : printf("No\n");     isPowerOfTwo(64) ? printf("Yes\n") : printf("No\n");     return 0; }

## 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 code     public static void main(String args[])     {           // Function call         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.

## Python3

 # Python program to check if given # number is power of 2 or not   # Function to check if x is power of 2     def isPowerOfTwo(n):     if (n == 0):         return False     while (n != 1):         if (n % 2 != 0):             return False         n = n // 2       return True     # Driver code if __name__ == "__main__":       # Function call     if(isPowerOfTwo(31)):         print('Yes')     else:         print('No')     if(isPowerOfTwo(64)):         print('Yes')     else:         print('No')   # This code is contributed by Danish Raza

## C#

 // C# program to find whether // a no is power of two using System;   class GFG {       // Function to check if     // x is power of 2     static bool isPowerOfTwo(int n)     {         if (n == 0)             return false;           while (n != 1) {             if (n % 2 != 0)                 return false;               n = n / 2;         }         return true;     }       // Driver code     public static void Main()     {         // Function call         Console.WriteLine(isPowerOfTwo(31) ? "Yes" : "No");         Console.WriteLine(isPowerOfTwo(64) ? "Yes" : "No");     } }   // This code is contributed by Sam007



## Javascript



Output

No
Yes

Time Complexity: O(log N)
Auxiliary Space: O(1)

## C++

 // C++ program for above approach #include using namespace std;   // Function which checks whether a number is a power of 2 bool powerOf2(int n) {     // base cases     // '1' is the only odd number which is a power of 2(2^0)     if (n == 1)         return true;       // all other odd numbers are not powers of 2     else if (n % 2 != 0 || n == 0)         return false;       // recursive function call     return powerOf2(n / 2); }   // Driver Code int main() {     int n = 64; // True     int m = 12; // False       // Function call     if (powerOf2(n) == 1)         cout << "True" << endl;       else         cout << "False" << endl;       if (powerOf2(m) == 1)         cout << "True" << endl;       else         cout << "False" << endl; }   // This code is contributed by Aditya Kumar (adityakumar129)

## C

 // C program for above approach #include #include   // Function which checks whether a number is a power of 2 bool powerOf2(int n) {     // base cases     // '1' is the only odd number which is a power of 2(2^0)     if (n == 1)         return true;       // all other odd numbers are not powers of 2     else if (n % 2 != 0 || n == 0)         return false;       // recursive function call     return powerOf2(n / 2); }   // Driver Code int main() {     int n = 64; // True     int m = 12; // False       // Function call     if (powerOf2(n) == 1)         printf("True\n");       else         printf("False\n");       if (powerOf2(m) == 1)         printf("True\n");       else         printf("False\n"); }   // This code is contributed by Aditya Kumar (adityakumar129)

## Java

 // Java program for // the above approach import java.util.*; class GFG {       // Function which checks     // whether a number is a     // power of 2     static boolean powerOf2(int n)     {         // base cases         // '1' is the only odd number         // which is a power of 2(2^0)         if (n == 1)             return true;           // all other odd numbers are         // not powers of 2         else if (n % 2 != 0 || n == 0)             return false;           // recursive function call         return powerOf2(n / 2);     }       // Driver Code     public static void main(String[] args)     {         // True         int n = 64;           // False         int m = 12;           // Function call         if (powerOf2(n) == true)             System.out.print("True"                              + "\n");         else             System.out.print("False"                              + "\n");           if (powerOf2(m) == true)             System.out.print("True"                              + "\n");         else             System.out.print("False"                              + "\n");     } }   // This code is contributed by Princi Singh

## Python3

 # Python program for above approach   # function which checks whether a # number is a power of 2     def powerof2(n):       # base cases     # '1' is the only odd number     # which is a power of 2(2^0)     if n == 1:         return True       # all other odd numbers are not powers of 2     elif n % 2 != 0 or n == 0:         return False       # recursive function call     return powerof2(n/2)     # Driver Code if __name__ == "__main__":           # Function call     print(powerof2(64))  # True     print(powerof2(12))  # False   # code contributed by Moukthik a.k.a rowdyninja

## C#

 // C# program for above approach using System;   class GFG {       // Function which checks whether a     // number is a power of 2     static bool powerOf2(int n)     {           // Base cases         // '1' is the only odd number         // which is a power of 2(2^0)         if (n == 1)             return true;           // All other odd numbers         // are not powers of 2         else if (n % 2 != 0 || n == 0)             return false;           // Recursive function call         return powerOf2(n / 2);     }       // Driver code     static void Main()     {           int n = 64; // True         int m = 12; // False           // Function call         if (powerOf2(n)) {             Console.Write("True"                           + "\n");         }         else {             Console.Write("False"                           + "\n");         }           if (powerOf2(m)) {             Console.Write("True");         }         else {             Console.Write("False");         }     } }   // This code is contributed by rutvik_56

## Javascript



Output

True
False

Time Complexity: O(log N)
Auxiliary Space: O(log N)

## Find whether a given number is a power of 2 by checking the count of set bits:

To solve the problem follow the below idea:

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

Below is the implementation of the above approach:

## C++

 // C++ program of the above approach   #include using namespace std;   /* Function to check if x is power of 2*/ bool isPowerOfTwo(int n) {     /* First x in the below expression is for the case when      * x is 0 */     int cnt = 0;     while (n > 0) {         if ((n & 1) == 1) {             cnt++;         }         n = n >> 1; // keep dividing n by 2 using right                     // shift operator     }       if (cnt == 1) { // if cnt = 1 only then it is power of 2         return true;     }     return false; }   // Driver code int main() {     // Function call     isPowerOfTwo(31) ? cout << "Yes\n" : cout << "No\n";     isPowerOfTwo(64) ? cout << "Yes\n" : cout << "No\n";     return 0; }   // This code is contributed by devendra salunke

## Java

 // Java program of the above approach import java.io.*;   class GFG {       // Function to check if x is power of 2     static boolean isPowerofTwo(int n)     {         int cnt = 0;         while (n > 0) {             if ((n & 1) == 1) {                 cnt++; // if n&1 == 1 keep incrementing cnt                 // variable             }             n = n >> 1; // keep dividing n by 2 using right                         // shift operator         }         if (cnt == 1) {             // if cnt = 1 only then it is power of 2             return true;         }         return false;     }       // Driver code     public static void main(String[] args)     {         // Function call         if (isPowerofTwo(30) == true)             System.out.println("Yes");         else             System.out.println("No");           if (isPowerofTwo(128) == true)             System.out.println("Yes");         else             System.out.println("No");     } }   // This code is contributed by devendra salunke.

## C#

 // C# program to check for power for 2 using System;   class GFG {       // Method to check if x is power of 2     static bool isPowerOfTwo(int n)     {         int cnt = 0; // initialize count to 0         while (n > 0) {               // run loop till n > 0             if ((n & 1) == 1) {                   // if n&1 == 1 keep incrementing cnt                 // variable                 cnt++;             }             n = n >> 1; // keep dividing n by 2 using right                         // shift operator         }           if (cnt             == 1) // if cnt = 1 only then it is power of 2             return true;         return false;     }       // Driver code     public static void Main()     {         // Function call         Console.WriteLine(isPowerOfTwo(31) ? "Yes" : "No");         Console.WriteLine(isPowerOfTwo(64) ? "Yes" : "No");     } }   // This code is contributed by devendra salunke

## Python3

 # Python3 program to check if given # number is power of 2 or not   # Function to check if x is power of 2     def isPowerOfTwo(n):     cnt = 0     while n > 0:         if n & 1 == 1:             cnt = cnt + 1         n = n >> 1       if cnt == 1:         return 1     return 0     # Driver code if __name__ == "__main__":       # Function call     if(isPowerOfTwo(31)):         print('Yes')     else:         print('No')       if(isPowerOfTwo(64)):         print('Yes')     else:         print('No')   # This code is contributed by devendra salunke

## Javascript



Output

No
Yes

Time complexity: O(log N)
Auxiliary Space: O(1)

## Find whether a given number is a power of 2 using the AND(&) operator:

To solve the problem follow the below idea:

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 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 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)) (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 the above approach:

## C++

 // C++ program for the above approach   #include using namespace std;   /* Function to check if x is power of 2*/ bool isPowerOfTwo(int x) {     /* First x in the below expression is for the case when      * x is 0 */     return x && (!(x & (x - 1))); }   // Driver code int main() {     // Function call     isPowerOfTwo(31) ? cout << "Yes\n" : cout << "No\n";     isPowerOfTwo(64) ? cout << "Yes\n" : cout << "No\n";     return 0; }   // This code is contributed by rathbhupendra

## C

 // C program for the above approach   #include #define bool int   /* Function to check if x is power of 2*/ bool isPowerOfTwo(int x) {     /* First x in the below expression is for the case when      * x is 0 */     return x && (!(x & (x - 1))); }   // Driver code int main() {     // Function call     isPowerOfTwo(31) ? printf("Yes\n") : printf("No\n");     isPowerOfTwo(64) ? printf("Yes\n") : printf("No\n");     return 0; }

## Java

 // Java program for the above approach   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 code     public static void main(String[] args)     {         // Function call         System.out.println(isPowerOfTwo(31) ? "Yes" : "No");         System.out.println(isPowerOfTwo(64) ? "Yes" : "No");     } } // This program is contributed by Gaurav Miglani

## Python3

 # Python3 program for the above approach   # Function to check if x is power of 2     def isPowerOfTwo(x):       # First x in the below expression     # is for the case when x is 0     return (x and (not(x & (x - 1))))     # Driver code if __name__ == "__main__":       # Function call     if(isPowerOfTwo(31)):         print('Yes')     else:         print('No')       if(isPowerOfTwo(64)):         print('Yes')     else:         print('No')   # This code is contributed by Danish Raza

## C#

 // C# program for the above approach using System;   class GFG {     // Method to check if x is power of 2     static bool 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 code     public static void Main()     {         // Function call         Console.WriteLine(isPowerOfTwo(31) ? "Yes" : "No");         Console.WriteLine(isPowerOfTwo(64) ? "Yes" : "No");     } }   // This code is contributed by Sam007



## Javascript



Output

No
Yes

Time Complexity: O(1)
Auxiliary Space: O(1)

## Find whether a given number is a power of 2 using the AND(&) and NOT(~) operator:

To solve the problem follow the below idea:

Another way is to use the logic to find the rightmost bit set of a given number and then check if (n & (~(n-1))) is equal to n or not

Below is the implementation of the above approach:

## C++

 // C++ program of the above approach   #include using namespace std;   /* Function to check if x is power of 2*/ bool isPowerofTwo(long long n) {     if (n == 0)         return 0;     if ((n & (~(n - 1))) == n)         return 1;     return 0; }   // Driver code int main() {     // Function call     isPowerofTwo(30) ? cout << "Yes\n" : cout << "No\n";     isPowerofTwo(128) ? cout << "Yes\n" : cout << "No\n";     return 0; } // This code is contributed by Sachin

## Java

 // Java program of the above approach import java.io.*;   class GFG {       // Function to check if x is power of 2     static boolean isPowerofTwo(int n)     {         if (n == 0)             return false;         if ((n & (~(n - 1))) == n)             return true;         return false;     }       // Driver code     public static void main(String[] args)     {         // Function call         if (isPowerofTwo(30) == true)             System.out.println("Yes");         else             System.out.println("No");           if (isPowerofTwo(128) == true)             System.out.println("Yes");         else             System.out.println("No");     } }   // This code is contributed by rajsanghavi9.

## Python3

 # Python program of the above approach   # Function to check if x is power of 2*/     def isPowerofTwo(n):       if (n == 0):         return 0     if ((n & (~(n - 1))) == n):         return 1     return 0     # Driver code if __name__ == "__main__":       # Function call     if(isPowerofTwo(30)):         print('Yes')     else:         print('No')       if(isPowerofTwo(128)):         print('Yes')     else:         print('No')   # This code is contributed by shivanisinghss2110

## C#

 // C# program of the above approach   using System; public class GFG {       // Function to check if x is power of 2     static bool isPowerofTwo(int n)     {         if (n == 0)             return false;         if ((n & (~(n - 1))) == n)             return true;         return false;     }       // Driver code     public static void Main(String[] args)     {           // Function call         if (isPowerofTwo(30) == true)             Console.WriteLine("Yes");         else             Console.WriteLine("No");           if (isPowerofTwo(128) == true)             Console.WriteLine("Yes");         else             Console.WriteLine("No");     } }   // This code contributed by gauravrajput1

## Javascript



Output

No
Yes

Time complexity: O(1)
Auxiliary Space: O(1)

## Find whether a given number is a power of 2 using Brian Kernighan’s algorithm:

To solve the problem follow the below idea:

As we know that the number which will be the power of two have only one set bit , therefore when we do bitwise AND with the number which is just less than the number which can be represented as the power of (2) then the result will be 0 .

Example : 4 can be represented as (2^2 ) ,
(4 & 3)=0  or in binary (100 & 011=0)

Below is the implementation of the above approach:

## C++

 // C++ program of the above approach   #include using namespace std;   /* Function to check if x is power of 2*/ bool isPowerofTwo(long long n) {     return (n != 0) && ((n & (n - 1)) == 0); }   // Driver code int main() {     // Function call     isPowerofTwo(30) ? cout << "Yes\n" : cout << "No\n";     isPowerofTwo(128) ? cout << "Yes\n" : cout << "No\n";     return 0; } // This code is contributed by Suruchi Kumari

## Java

 // Java program of the above approach   import java.io.*; class GFG {       /* Function to check if x is power of 2*/     public static boolean isPowerofTwo(long n)     {         return (n != 0) && ((n & (n - 1)) == 0);     }       // Driver code     public static void main(String[] args)     {         // Function call         if (isPowerofTwo(30)) {             System.out.println("Yes");         }         else {             System.out.println("No");         }           if (isPowerofTwo(128)) {             System.out.println("Yes");         }         else {             System.out.println("No");         }     } }   // This code is contributed by akashish__

## Python3

 # Python3 program of the above approach   # Function to check if x is power of 2     def isPowerofTwo(n):       return (n != 0) and ((n & (n - 1)) == 0)     # Driver code if __name__ == "__main__":           # Function call     if isPowerofTwo(30):         print("Yes")     else:         print("No")       if isPowerofTwo(128):         print("Yes")     else:         print("No")   # this code is contributed by aditya942003patil

## C#

 // C# program of the above approach   using System;   public class GFG {     /* Function to check if x is power of 2*/     static public bool isPowerofTwo(ulong n)     {         return (n != 0) && ((n & (n - 1)) == 0);     }       // Driver code     static public void Main()     {           // Function call         if (isPowerofTwo(30)) {             System.Console.WriteLine("Yes");         }         else {             System.Console.WriteLine("No");         }           if (isPowerofTwo(128)) {             System.Console.WriteLine("Yes");         }         else {             System.Console.WriteLine("No");         }     } }   // This code is contributed by akashish__

## Javascript



Output

No
Yes

Time Complexity: O(1)
Auxiliary Space: O(1)

## Find whether a given number is a power of 2 using a floating point bit hack:

We can also harness a unique property of IEEE Standard 754 to infer if the given integer is a power of 2 using the following bit hack that only works in a few languages that allow pointer casting.

This works because we know that a power of 2 only has 1 bit high and all the other bits low. Therefore if we represent such a number in scientific notation, we’ll always be left with a mantissa of 1. But in IEEE Standard 754 the 1 is discarded from the mantissa as it is redundant. Now we should be left with 0, if not, then the number must not be a power of 2. We will be using double precision.

Example:

Let’s take 23 first
23 = 00010111
=1.0111000 x 2^4

Biased Exponent 1023+4=1027
1027 = 10000000011
Normalised Mantissa = 01110000
We will add 0’s to complete the 52 bits

The IEEE 754 Double Precision is:
= 0 10000000011 0111000000000000000000000000000000000000000000000000

Notice that the mantissa is not 0.

——————————————————————————————

Now let’s take a power of 2, say 16
16 = 00010000
=1.0000000 x 2^4

Biased Exponent 1023+4=1027
1027 = 10000000011
Normalised Mantissa = 00000000
We will add 0’s to complete the 52 bits

The IEEE 754 Double Precision is:
= 0 10000000011 0000000000000000000000000000000000000000000000000000

Now the mantissa is strictly 0.

——————————————————————————————

Below is the implementation of the above approach:

## C

 #include #include    // Function to check if x is power of 2. int isPowerOfTwo(int x) {     // Power of 2 can't be less than 1.     if (x < 1)       return 0;     // Converting the number to double precision floating point.     double n = x;     /*      * Storing the binary representation of double to an unsigned 64 bit integer.      * This is not the same as direct casting to integer, as the binary representation changes.      */     uint64_t r = *((uint64_t*)(&n));     // Discarding 1 sign bit and 11 exponent bits.     r = (r << 12);     return r == 0; }    // Driver code int main() {     // Function calls     isPowerOfTwo(31) ? printf("Yes\n") : printf("No\n");     isPowerOfTwo(32) ? printf("Yes\n") : printf("No\n");     isPowerOfTwo(33) ? printf("Yes\n") : printf("No\n");     isPowerOfTwo(64) ? printf("Yes\n") : printf("No\n");     return 0; } // This code is contributed by Aryan Rai.

## C++

 #include   using namespace std;   // Function to check if x is power of 2. bool isPowerOfTwo(int x) {     // Power of 2 can't be less than 1.     if (x < 1)       return false;     // Converting the number to double precision floating point.     double n = x;     /*      * Storing the binary representation of double to an unsigned 64 bit integer.      * This is not the same as direct casting to integer, as the binary representation changes.      */     uint64_t r = *((uint64_t*)(&n));     // Discarding 1 sign bit and 11 exponent bits.     r = (r << 12);     return r == 0; }    // Driver code int main() {     // Function calls             cout << ( isPowerOfTwo(31) ? "Yes" : "No" ) << endl;     cout << ( isPowerOfTwo(32) ? "Yes" : "No" ) << endl;     cout << ( isPowerOfTwo(33) ? "Yes" : "No" ) << endl;     cout << ( isPowerOfTwo(64) ? "Yes" : "No" ) << endl;     return 0; } // This code is contributed by Aryan Rai.

Output

No
Yes
No
Yes

Time Complexity: O(1)
Auxiliary Space: O(1)

Please write comments if you find anything incorrect, or if you want to share more information about the topic discussed above.

My Personal Notes arrow_drop_up
Related Articles