PHP位与(安培)返回负数 [英] PHP bitwise AND (&) returns negative number

查看:227
本文介绍了PHP位与(安培)返回负数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在code:

echo (5243960811416 & 4040906070209050);

尝试在 http://phptester.net/ 和结果(右)将 20407554584

但我的虚拟主机它给出了 -1067281896

but on my web hosting it gives -1067281896.

有任何解决方法有 20407554584 ?这是一个32位的限制?

There's any workaround to have 20407554584? It's a 32bit limit?

感谢

<一个href=\"http://stackoverflow.com/questions/30387305/php-bitwise-and-returns-negative-number/30447049#30447049\">UPDATE / w溶液

推荐答案

SOLUTION

在searchs和searchs我发现<一个href=\"http://stackoverflow.com/questions/3637702/how-to-do-bitwise-and-in-javascript-on-variables-that-are-longer-than-32-bit#3638080\">this我在PHP重新转换

After searchs and searchs I found this and I re-convert in PHP

function BitwiseAndLarge($val1, $val2) {

    $shift = 0; 
    $result = 0;
    $mask = ~((~0) << 30); // Gives us a bit mask like 01111..1 (30 ones)

    $divisor = 1 << 30; // To work with the bit mask, we need to clear bits at a time

    while( ($val1 != 0) && ($val2 != 0) ) {
        $rs = ($mask & $val1) & ($mask & $val2);
        $val1 = floor($val1 / $divisor); // val1 >>> 30
        $val2 = floor($val2 / $divisor); // val2 >>> 30

        for($i = $shift++; $i--;) {
            $rs *= $divisor; // rs << 30
        }

        $result += $rs;
    }
    return $result;
}

使用

echo BitwiseAndLarge(5243960811416 & 4040906070209050);

这篇关于PHP位与(安培)返回负数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆