Checking if a number is a power of 2 without loops I made a short program which checks if a number is a power of 2 without using any loops.
The idea: A number which is a power of 2 must have only one bit "1" ( ex: 8= 1000, 4=100 and so on). 
Suppose we have a power of 2:nr = 10...000 (in binary), if we subtract 1 we will get something like this:nr-1= 01...111. Now, if we do nr&amp;(nr-1) we should always get 0 if the nr is a power of 2 and some random number if it isn't. What other solutions are there for this problem?

#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;

int main()
{
    int nr;
    scanf("%d",&amp;nr);
    if((nr&amp;(nr-1))==0)
    {
        printf("\n%d is a power of 2",nr);
    }
    else
    {
         printf("\n%d is not a power of 2",nr);
    }
    return 0;
}
Use only necessary #includes. The &lt;stdlib.h&gt; is not needed here.
Give your operators some breathing space. ((nr&amp;(nr-1))==0) is next to unreadable.
Separate logic from presentation:

int is_power_of_two(int nr)
{
    return nr &amp; (nr - 1) == 0;
}


is much more reusable.
Care about corner cases. Your code claims that 0 is a power of two (which it is not).