PHP equivalent javascript >>> shift right with zero fill bitwise operators?

后端 未结 8 1312
孤城傲影
孤城傲影 2021-01-13 19:43

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:

         


        
相关标签:
8条回答
  • 2021-01-13 20:36

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

    Have a look:

    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;
    }
    
    0 讨论(0)
  • 2021-01-13 20:39

    Twice as fast for negative numbers as using the decimal-binary conversions

    function zerofill($a,$b) { 
        if($a>=0) return $a>>$b;
        if($b==0) return (($a>>1)&0x7fffffff)*2+(($a>>$b)&1);
        return ((~$a)>>$b)^(0x7fffffff>>($b-1)); 
    
    0 讨论(0)
提交回复
热议问题