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

后端 未结 8 1473
孤城傲影
孤城傲影 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:18

    For both 32-bit (PHP_INT_SIZE == 4) and 64-bit integers (PHP_INT_SIZE == 8):

    function SHR
    ($x, $c)
    {
        $x = intval ($x); // Because 13.5 >> 0 returns 13. We follow.
    
        $nmaxBits = PHP_INT_SIZE * 8;
        $c %= $nmaxBits;
    
        if ($c)
            return $x >> $c & ~ (-1 << $nmaxBits - $c);
        else
            return $x;
    }
    

提交回复
热议问题