You are here: Re: Check if power of 2 algorithm ??? « PHP Programming Language « IT news, forums, messages
Re: Check if power of 2 algorithm ???

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]


Удаленная работа для программистов  •  Как заработать на Google AdSense  •  England, UK  •  статьи на английском  •  PHP MySQL CMS Apache Oscommerce  •  Online Business Knowledge Base  •  DVD MP3 AVI MP4 players codecs conversion help
Home  •  Search  •  Site Map  •  Set as Homepage  •  Add to Favourites

Copyright © 2005-2006 Powered by Custom PHP Programming

Сайт изготовлен в Студии Валентина Петручека
изготовление и поддержка веб-сайтов, разработка программного обеспечения, поисковая оптимизация