Check whether a number is power of 2

Q. Give one line C expression to check whether a given number is an exponential power of 2

Ans:
if ( x & (x-1) == 0){
printf(“Yes”)
}else{
printf(“No”)
}
What about zero ??

Set the LSB to zero
x = x&x(-1);

Leave a Reply

Your email address will not be published. Required fields are marked *