How to check a number is power of 2

Let us see how to check a number is power of 2 in a single line as shown in the program below
Program:
BOOLEAN isPowerOfTwo(int num)
{
   return ( num && ( !(num & (num - 1) ) ) );
}
Let us understand the above function with the following example
Apply different numbers for num in the above function

num = 8
8 && ( !( 8 & (7) ) )  => 1000 && ( !( 1000 & (0111) ) )  => 1 && ( ! 0 ) => 1 && 1 => TRUE

num = 6
6 && ( !( 6 & (5) ) )  => 0110 && ( !( 0110 & (0101) ) )  => 1 && ( ! 0100 ) => 1 && 0 => FALSE

num = 0
0 && ( !( 0 & (0) ) ) => 0 && 0 => FALSE

Here, now you could understand why num condition is added first ( num && ( !(num & (num - 1) ) ) ) to send 0(false) for number 0. When the number is logical AND (&) with the previous number (num -1) it will false when the number is power of 2. As the power of two - 1 should have all the bits set.