简体   繁体   English

使用PHP计算32位系统上的按位64位运算

[英]Calculate bitwise 64bit operations on 32bit system with PHP

I have a series of base 10 representations of binary numbers stored in a database. 我有一系列存储在数据库中的二进制数的基本10表示。 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. 现在我知道这个二进制数大于32位,因此我得不到我期望的结果。

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. 它似乎是一个32位操作系统问题,但我似乎无法弄清楚如何将$code值分成两个32位段。

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 然后我用$lsb$msb替换$code

can someone point me in the right direction so that i can get all the values for n. 有人能指出我正确的方向,以便我可以获得n的所有值。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM