Logic- when u convert a decimal number to its binary number then u get series of 1 and 0 ,Now if a number is power of two then u will observe it has only one (bit 1) and if you perform binary and operation of it with just a number previous to it ,then u will get Zeros.
For example
8=1000(binary form ) here u can see only one (bit 1) is there rest are zeros,
Now the number just previous to 8 is 7
so, 7=0111 and if u perform binary and operation on it as
1000(8)
(AND) 0111(7)
———
0000(0)
———
So if we perform AND operation between the number (which we want to check is a power of two or not) and the number just previous to it and result comes as zeros then its power of two otherwise it is not.
so here is the one line expression
if(!(num & (num-1))&&num)
if the above condition true then the number is of power of two otherwise it is not.