What is the fastest way to do this?
[code:1]bool ispoweroftwo(int n) { return !(n&(n-1)); }[/code:1] if a number is power of two its bit pattern is 000001000000 and that number -1 will be 000000111111. AND them and you will get 0
Determine if a given number is a power of 2?
[code:1]bool ispoweroftwo(int n)
{
return !(n&(n-1));
}[/code:1]
if a number is power of two its bit pattern is 000001000000
and that number -1 will be 000000111111. AND them and you will get 0