note 40994 deleted from language.operators.bitwise by danielc
| From: | danielc@php.net | Date: | Mon, 13 Apr 2009 16:10:08 +0000 |
| Subject: | note 40994 deleted from language.operators.bitwise by danielc | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-153153@lists.php.net to get a copy of this message | ||
Note Submitter: lars dot jensen at ljweb dot com
Reason: 2
----
A useful set of functions, making it easier to play with bits as a set of booleans. $bit is a
integer between 1 and 32 representing the position of the bit you wish to manipulate.
<?php
function setbit($val, $bit) {
if (readbit($val, $bit)) return $val;
return $val += '0x'.dechex(1<<($bit-1));
}
function clearbit($val, $bit) {
if (!readbit($val, $bit)) return $val;
return $val^(0+('0x'.dechex(1<<($bit-1))));
}
function readbit($val, $bit) {
return ($val&(0+('0x'.dechex(1<<($bit-1)))))?'1':'0';
}
function debug($var, $bitlength=32) {
for ($j=$bitlength;$j>0;$j--) {
echo readbit($var, $j);
if ($j%4 == 1) echo ' ';
}
}
?>