Bitmasking Question POWER(O(LOGN))

How can i calculate x raise to power n using bitmasking approach?

Basically you want to calculate a^b , so–>
Suppose your answer variable is initialised with 1 (ans=1) and you can iterate on the bits of b and mulltiply the contribution of the current bit to our final answer

int power(int a,int b){
int ans=1;
int pp=a;
while(b!=0){
if(b&1)// if current bit is set or not {
ans*=(pp) ;
}
pp=pp*pp;
}
return ans; // Finally return the calculated answer .
}

you forgot doing b=b>>1;
after pp = pp * pp;