- assert: documentation ( source)
- decbin: documentation ( source)
- print_r: documentation ( source)
<?php
function getExclusiveORValue()
{
$args = func_get_args();
$ret = 0;
foreach ($args as $arg)
{
assert('($arg >= 0) && ($arg < PHP_INT_SIZE*8)');
$ret |= 1 << $arg;
}
return $ret;
}
function getArrayOfItemsFromExclusiveORValue($bits)
{
$arr = array();
for ($i=0; $i<PHP_INT_SIZE*8; $i++)
{
$shift = $bits >> $i;
if ($shift & 1)
{
$arr[] = $i;
}
if ($shift == 0)
{
break;
}
}
return $arr;
}
$bits = getExclusiveORValue(0, 1, 3, 8, 31);
echo decbin($bits) . PHP_EOL;
// 10000000000000000000000100001011
$arr = getArrayOfItemsFromExclusiveORValue($bits);
print_r($arr);
/**
Array
(
[0] => 0
[1] => 1
[2] => 3
[3] => 8
[4] => 31
)
**/