27.2.08

power of 2

This is one tricky bit usually asked in C interviews

How to tell the given number is Power of 2 or not in a one line 'C 'code.

There are many ways to do this, here is my logic.

Code snippet:

int num; /* holds the integer value */

num & (num-1) ? printf("Not Power of 2\n") : printf("Power of 2\n");