Re: Test for one bit set?
| From: | Lars Torben Wilson | Date: | Sun, 07 Apr 2002 21:41:50 +0000 |
| Subject: | Re: Test for one bit set? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-91955@lists.php.net to get a copy of this message | ||
On Sun, 2002-04-07 at 13:38, 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.
>
> TIA
>
> Charlie
AND them bitwise:
<?php
error_reporting(E_ALL);
$bit = 16;
$foo = bindec('00010000');
$bar = bindec('00000011');
echo "$foo; $bar\n";
if ($foo & $bit)
{
echo "Bit 5 is set.\n";
}
else
{
echo "Bit 5 is not set.\n";
}
if ($bar & $bit)
{
echo "Bit 5 is set.\n";
}
else
{
echo "Bit 5 is not set.\n";
}
?>
Torben
--
Torben Wilson <torben@php.net>
http://www.thebuttlesschaps.com
http://www.hybrid17.com
http://www.inflatableeye.com
+1.604.709.0506