I have an array of 1000+ items. the value in each index is either 1 or 0. I want to be able to do bitwise operations such as counting the number of 1's and also finding the 1's location on this array which would entail converting the array into a binary number and then into a decimal number and performing bitwise operations (which would be very fast and efficient)
So imagine something like this:
$array = array(
"foo" => 1,
"bar" => 0,
"test"=> 0,
...
);
I can use implode to convert the array values into a string.
$str = implode('', $array);
This gives me 100. but in order for me to do any bitwise operation I first need to turn this in to a decimal number and if the size of the array (number of bits) is more than 64 (64 bit machine), things don't work:
$b = bindec($str); // will fail
$c = $b | 7;
Similarly if I have to do any shifting or counting of 1's etc. things don't work.
Anyone has any idea on how to overcome this problem in php.
array_sum($array)for counting the 1s? And usingarray_unshift($array, array_pop($array))andarray_push($array,array_shift($array))for shifting?