note 107617 added to language.operators.bitwise
| From: | daevid at daevid dot com | Date: | Wed, 22 Feb 2012 04:04:55 +0000 |
| Subject: | note 107617 added to language.operators.bitwise | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-186269@lists.php.net to get a copy of this message | ||
<?php
/**
* Get the upper and lower boundaries for any given number and 2^n offset (other offsets give
bizarre results)
*
* @access public
* @return array
* @param integer $number
* @param integer $offset (32) must be a 2^n value [8, 16, 32, 64, 128, ...)
* @author Daevid Vincent
* @date 2012-02-21
*/
function boundaries_ul($number, $offset=32)
{
if ($offset < 8) return false; //nothing smaller than this makes practical sense
if (($offset & ($offset - 1)) != 0) return false; //not a 2^n
$offset = $offset - 1;
//zero out least significant bits.
//For example if $offset = 32, then the least 5 bits [16 8 4 2 1] == 31 == 0xFFE0
$lowerbound = $number & ~$offset;
$upperbound = $lowerbound + $offset;
return array('lower_bound'=>$lowerbound, 'upper_bound'=>$upperbound);
}
?>
Example:
<?php
for ($i = 0; $i < 20; $i++)
{
debug_print($i, 'forloop $i decimal');
$b = boundaries_ul($i, 8);
var_dump($b);
echo "<hr>\n";
}
?>
forloop $i decimal = 0
array
'lower_bound' => int 0
'upper_bound' => int 7
forloop $i decimal = 1
array
'lower_bound' => int 0
'upper_bound' => int 7
forloop $i decimal = 2
array
'lower_bound' => int 0
'upper_bound' => int 7
...
forloop $i decimal = 12
array
'lower_bound' => int 8
'upper_bound' => int 15
forloop $i decimal = 13
array
'lower_bound' => int 8
'upper_bound' => int 15
...
----
Server IP: 69.147.83.197
Probable Submitter: 207.178.4.251
----
Manual Page -- http://www.php.net/manual/en/language.operators.bitwise.php
Edit -- https://master.php.net/note/edit/107617
Del: integrated -- https://master.php.net/note/delete/107617/integrated
Del: useless -- https://master.php.net/note/delete/107617/useless
Del: bad code -- https://master.php.net/note/delete/107617/bad+code
Del: spam -- https://master.php.net/note/delete/107617/spam
Del: non-english -- https://master.php.net/note/delete/107617/non-english
Del: in docs -- https://master.php.net/note/delete/107617/in+docs
Del: other reasons-- https://master.php.net/note/delete/107617
Reject -- https://master.php.net/note/reject/107617
Search -- https://master.php.net/manage/user-notes.php