|
Posted by bellefy@gmail.com on 05/19/06 21:07
Hi All,
I have a question about a formula I found online at
http://graphics.stanford.edu/~seander/bithacks.html#DetermineIfPowerOf2.
To determine if an integer is a power of 2, the author states that:
f = (v & (v -1)) == 0;
I am very proficient with scripting techniques using PHP so I
understand what we're trying to accomplish here.
However, I can't seem to figure out how to make the example work... :-(
Take, for example, 16.
f = (16 & (16 - 1)) == 0;
f = (16 & (15)) == 0;
The test is always false????????
I think I am not understanding the steps completely...
For reference, in PHP, there is no operator &. The logical AND operator
is &&.
In PHP, the same formula would be written as:
if((v && (v-1)) == 0){
return true;
}
Can anyone explain this to me? I would greatly appreciate any help.
Thanks in advance,
Tim Bellefy
Navigation:
[Reply to this message]
|