Possible Duplicates:
Query about working out whether number is a power of 2
How to check if a number is a power of 2
I require a function body for this prototype:
bool isPOT(int x);
So it would return eg isPOT(3) = FALSE, but isPOT(8) = TRUE
What is the most pretty/concise algorithm? And what is the most efficient?
PS: I am amazed that I cannot find this question on SO, so I am fully expecting someone to detect some duplicate.
PPS: can someone please create POT, NPOT, Power-Of-Two tags?
bool IsPOT(int x)
{
return (x > 0) && ((x & (x - 1)) == 0);
}
Not sure if this exact question occurred, but the check is easy
x & (x - 1) == 0
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With