Original question:
"Give a one-line C expression to test whether a number
is a power of 2. [No loops allowed - it's a simple test.]"
Answer:
if (x && !(x & (x-1)) == 0)
My question:
Why does this expression work?
Thanks.
"Give a one-line C expression to test whether a number
is a power of 2. [No loops allowed - it's a simple test.]"
Answer:
if (x && !(x & (x-1)) == 0)
My question:
Why does this expression work?
Thanks.
Comment