Power of 2 | Practice | GeeksforGeeks

Given a non-negative integer N. The task is to check if N is a power of 2. More formally, check if N can be expressed as 2x for some x.
Example 1:
Input: N = 1
Output: YES
Explanation:1 is equal to 2 
raised to 0 (20 = 1).
Example 2:
Input: N = 98
Output: NO
Explanation: 98 cannot be obtained
by any power of 2.

Your Task:Your task is to complete the function isPowerofTwo() which takes n as a parameter and returns true or false by checking if the given number can be represented as a power of two or not.

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

Constraints:
0 ≤N ≤1018
0
raunakgiri2110 hours ago
return (!n || n&(n-1)) ? 0 : 1;
0
vishnu12654 days ago
public static boolean isPowerofTwo(long n){
        boolean res = false;
        while(n>0){
            if(n%2==1 && res==false){
                res = true;
            }
            else if(n%2==1 && res==true){
                res = false;
                break;
            }
            n = n/2;
        }
        return res;
        
    }
0
mitradiptamoy1 week ago
//C++ soln 
 
bool isPowerofTwo(long long n){              // Your code here           if(n<=0){           return false;       }       else{           return ((n&(n-1))==0)?true:false;       }          }
+1
bharathimp19931 week ago
Java Solution:
 
class Solution{      // Function to check if given number n is a power of two.   public static boolean isPowerofTwo(long n){       if(n<=0)        return false;     return ((n&(n-1))==0)?true:false;          }   }
+2
mankesh0161 week ago
return n && !(n & n-1);
0
allamnenirohith1 week ago
class Solution:   ##Complete this function   # Function to check if given number n is a power of two.   def isPowerofTwo(self,n):       ##Your code here       for i in range(n):           if 2**i==n:               return 'YES'           if 2**i>n:               return 'NO'
what is wrong in this code?
0
yashvardhanyadav81 week ago
 
for (int i=0; i<=60; i++) {           if (pow(2,i)==n) {               return true;           }       } return false;
0
asifsaba5812 weeks ago
class Solution{
    public:
    // Function to check if given number n is a power of two.
    bool isPowerofTwo(long long n){
        
        // time complexity of this algo. is O(1);
       
       // by using braian karningan algo.
       
       //A num. is a power of 2 when the setbit of this num is 1.
       
       // let say N=32;
       
       //00100000(32) & 00011111(31) = 00000000(0);
       
       //hence, 32 has 1 setbit that's why it is a power of 2.
       
       
       if(n==0){
           
        return false;
           
       }
       int ans= n&(n-1);
       
       
       if(ans==0){
           
           return true;
       }
       
       return false;
       
    }
};

+1
anindiangeek2 weeks ago
It can't get any shorter than this and it is fastest but works only with GCC.
 
return __builtin_popcountll(n)==1;
 
0
keshavkumar2 weeks ago
MOst efficient O(1) time complexity
 
bool isPowerofTwo(long long n){              // Your code here       if(n==0)           return false;       int ans= n&(n-1);       if(ans==0){           return true;       }       else           return false;          }
We strongly recommend solving this problem on your own before viewing its editorial. Do you still
                    want to view the editorial?
 Login to access your submissions. 

Problem


Contest

Reset the IDE using the second button on the top right corner.
Avoid using static/global variables in your code as your code is tested against multiple test cases and these tend to retain their previous values.
Passing the Sample/Custom Test cases does not guarantee the correctness of code. On submission, your code is tested against multiple test cases consisting of all possible corner cases and stress constraints.
You can access the hints to get an idea about what is expected of you as well as the final solution code.
You can view the solutions submitted by other users from the submission tab.