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

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

    Your function doesn't work because when $b == 0, the expression

    $a >> -1
    

    will be evaluated, which returns 0.

    Assuming 32-bit machines, you can add a special case:

    if ($z & $a) {
      if ($b == 0)
        return $a + 0x100000000;
      else {
        ...
    

提交回复
热议问题