On Jun 11, 6:29 am, Shraddha <shraddhajosh...@gmail.comwrote:
How to find the power of two (2 rais to n) in a single step???
I am guessing that what you are really asking is how to find out if an
integer number is an integral power of 2.
A simple way to do that is to do a bitcount and see if it is 1.
Maybe something like this:
int bitcount (unsigned long n)
{
n = (n & 0x55555555) + ((n >1) & 0x55555555);
n = (n & 0x33333333) + ((n >2) & 0x33333333);
n = (n + (n >4)) & 0x0f0f0f0f;
n += n >8;
n += n >16;
return (n & 0xff);
}
then...
if (bitcount(n) == 1) puts("It's a power of 2");
else puts("It is not a power of 2");