note 107617 deleted from language.operators.bitwise by tularis

From: Date: Wed, 22 Feb 2012 21:02:19 +0000
Subject: note 107617 deleted from language.operators.bitwise by tularis
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-186291@lists.php.net to get a copy of this message
Note Submitter: daevid at daevid dot com ---- <?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 ...

« previous php.notes (#186291) next »