Posted by Dana Cartwright on 05/21/06 17:50
"David Haynes" <david.haynes2@sympatico.ca> wrote in message
news:_DYbg.93018$6.44147@fe82.usenetserver.com...
> Toby Inkster wrote:
>> bellefy@gmail.com wrote:
>>
>>> $r = ($v && ($v) == 0 ? true : false);
>>
>> Better:
>>
>> $r = (($v & ($v-1)) == 0);
>>
>> NEVER code '?true:false' (or the opposite).
>>
> This makes 0 a power of two...
>
> Try:
> $r = !($v & ($v-1)) && $v;
>
Since it also fails for negative numbers, you might as well include that
case:
$r = !($v & ($v-1)) && ($v > 0);
- Dana
Navigation:
[Reply to this message]
|