Re: bit wise math? Is there a function to easily return the bits?

From: Date: Thu, 25 Jan 2007 23:10:16 +0000
Subject: Re: bit wise math? Is there a function to easily return the bits?
References: 1 2  Groups: php.general 
Request: Send a blank email to php-general+get-247795@lists.php.net to get a copy of this message
Jon Anderson wrote:
function bits($num) {
       $bit_array = str_split(strrev(decbin(intval($num))));
       $val_array = array();
       foreach ($bit_array as $pow => $bit) {
               if ($val = $bit * pow(2,$pow))
                       $val_array[] = $val;
       }
       return($val_array);
} (I wanted to see if I could write it in few LOC.) I wonder if there's a faster way... jon blackwater dev wrote:
Is there a php function I can call to pass in a number and get the values returned? For example, pass in 7 and get 1,2,4 ? Thanks!
and for those of us running PHP<5.x here is a working example
function bits($num) { $bit_array = split('.',strrev(decbin(intval($num)))); $val_array = array(); foreach ($bit_array as $pow => $bit) { if ($val = $bit * pow(2,$pow)) $val_array[] = $val; } return $val_array; } -- Enjoy, Jim Lucas Different eyes see different things. Different hearts beat on different strings. But there are times for you and me when all such things agree. - Rush

« previous php.general (#247795) next »