/* Power of 4 */
/*
Given an integer, check if it is a power of 4
Approach : Check if it is power of 2 and the number of zeros is even
*/
/*
Given an integer, check if it is a power of 4
Approach : Check if it is power of 2 and the number of zeros is even
*/
#include<iostream>
using namespace std;
bool isPowerOfFour(int num) {
if(num < 0)
return false;
//power of 2
if(num && !(num & (num - 1))){
int count = 0;
while(num > 1){
num = num >> 1;
count++;
}
//if number of zeros is even, return true
if(count % 2 == 0){
return true;
}
}
return false;
}
int main(){
int n = 15;
cout << isPowerOfFour(n);
return 0;
}
0 comments:
Post a Comment