On Sun, 7 Apr 2002, Charlie Killian wrote:
> How can I test if a number only has on bit set?
>
> So testing different numbers will return TRUE or FALSE:
>
> testing 00010000 would return TRUE.
> testing 00000011 would return FALSE.
Think back to math class when you were 14!
function isOneBitSet($n)
{
$x = log($n)/log(2);
return ($x == intval($x));
}
Cheesy alternative:
function isOneBitSet($n)
{
return (1 == substr_count(base_convert($n, 10, 2), '1'));
}
miguel
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php