Reply to Check if power of 2 algorithm ???

Your name:

Reply:


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

[Back to original 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

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