PHP相当于JavaScript的>>>右移带填零位运算符? [英] PHP equivalent javascript >>> shift right with zero fill bitwise operators?

查看:239
本文介绍了PHP相当于JavaScript的>>>右移带填零位运算符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以知道我该怎么办PHP​​ >>>?这样的运营商是不是在PHP中使用,但可在Javascript。

May I know how can I do PHP >>> ? Such operators is not available in PHP, but is available in Javascript.

我好不容易才找到一个函数如下:

I just managed to discover a function as follow:

function zeroFill($a, $b) 
{ 
    $z = hexdec(80000000); 
        if ($z & $a) 
        { 
            $a = ($a>>1); 
            $a &= (~$z); 
            $a |= 0x40000000; 
            $a = ($a>>($b-1)); 
        } 
        else 
        { 
            $a = ($a>>$b); 
        } 
        return $a; 
}

但不幸的是,它并不完美。

but unfortunately, it doesn't work perfectly.

EG:-1149025787 >>> 0
JavaScript的返回3145941509
PHP ZEROFILL()返回0

EG: -1149025787 >>> 0 Javascript returns 3145941509 PHP zeroFill() return 0

推荐答案

我研究周围的网,用我自己的ZEROFILL功能基础上给出的解释出来。此方法适用于我的计划。

I studied around the webs and come out with my own zerofill function, base on the explanation given. This method works for my program.

看一看:

function zeroFill($a,$b) {
    if ($a >= 0) { 
        return bindec(decbin($a>>$b)); //simply right shift for positive number
    }

    $bin = decbin($a>>$b);

    $bin = substr($bin, $b); // zero fill on the left side

    $o = bindec($bin);
    return $o;
}

这篇关于PHP相当于JavaScript的>>>右移带填零位运算符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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