//C program for above approach
#include <iostream>
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 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(56) ? cout << "Yes\n" : cout<< "No\n";
isPowerOfTwo(128) ? cout << "Yes\n" : cout<< "No\n";
return 0;
}
To embed this program on your website, copy the following code and paste it into your website's HTML: