Question

I have a series of base 10 representations of binary numbers stored in a database. for example:

686316060672

Now i have written some simple code to extract the bits from this:

$code = 686316060672;

for($n = 0; $n < 64; $n++)
{
    $r = pow(2,$n);

    if(($code  & $r) > 0)
    {
        printf('n=%d'."\n", $n);
    }
}

Now i know this number in binary is larger than 32bit, and thus im not getting the results i would expect.

the above code gives me:

n=21 n=23 n=24 n=25 n=27 n=30

when i know the answer should be:

n=21 n=23 n=24 n=25 n=27 n=30 n=31 n=32 n=33 n=34 n=35 n=36 n=39

it would seem that its a 32bit operating system issue, but for i cant seem to work out how to split the $code value into two 32 bit segments.

this is what i tried:

$number =  base_convert($code, 10, 2);
$lsb = bindec(substr($number, -32));
$msb = bindec(substr_replace($number, '', -33));

I then replace $code with $lsb and $msb

can someone point me in the right direction so that i can get all the values for n.

Was it helpful?

Solution

So after looking into ita bit more.. i was looking at the problem the wrong way. below is how i solved the problem.

$x = 686316060672;
$y = 2;


for($n = 1; $n < 65; $n++){
$x = floor($x / $y); 
if($x % 2){
printf('n=%d'."\n", $n);
}// end if

}// end for

this gives:

n=21 n=23 n=24 n=25 n=27 n=30 n=31 n=32 n=33 n=34 n=35 n=36 n=39
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top