note 17895 deleted from function.isset by jimw
| From: | jimw@php.net | Date: | Wed, 02 Jan 2002 12:20:16 +0000 |
| Subject: | note 17895 deleted from function.isset by jimw | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-24276@lists.php.net to get a copy of this message | ||
I couldn't find a function which checks if a bit is set in a
bit pattern / integer value. So I wrote one myself.
function isbitset($myint, $mybit, $mybitsize) {
$mystring = substr(str_repeat("0", $mybitsize) . decbin($myint), -$mybitsize, $mybitsize);
$mysubstr = substr($mystring, $mybit, 1);
return ($mysubstr || !$mysubstr == 0) ? true : false;
}
The function will return true if the bit in question is set,
or false if it it isn't set.
$myint
------
The integer value that you want to check its bits.
$mybit
------
The bit number you want to check whether set or not,
where $mybit = 0 means you are checking the msb, in
other words the first bit from the left.
$mybitsize
----------
The number of bits which the value $myint could consist
of, when all bits would be set. For example, to test bit
number 0 (msb) of a bitsize of 8 would result true, while
the same test of bit number 0 of a bitsize of 16 would result
false if $myint was 255.
See for yourself whether useful and without bugs.
I don't know if there's a better approach than this,
just let me know.
Greets, Jeroen.