Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在PHP中执行正确的无符号右移?_Javascript_Php_Bit Shift_Unsigned Integer - Fatal编程技术网

Javascript 如何在PHP中执行正确的无符号右移?

Javascript 如何在PHP中执行正确的无符号右移?,javascript,php,bit-shift,unsigned-integer,Javascript,Php,Bit Shift,Unsigned Integer,这是否可能在PHP和Javascript中获得相同的结果 例如: Javascript <script> function urshift(a, b) { return a >>> b; } document.write(urshift(10,3)+"<br />"); document.write(urshift(-10,3)+"<br />"); document.write(urshift(33, 33)+"<br /&g

这是否可能在PHP和Javascript中获得相同的结果

例如:

Javascript

<script>

function urshift(a, b)
{
  return a >>> b;
}

document.write(urshift(10,3)+"<br />");
document.write(urshift(-10,3)+"<br />");
document.write(urshift(33, 33)+"<br />");
document.write(urshift(-10, -30)+"<br />");
document.write(urshift(-14, 5)+"<br />");

</script>
PHP

function uRShift($a, $b) 
    { 
        if ($a < 0) 
        { 
            $a = ($a >> 1); 
            $a &= 2147483647; 
            $a |= 0x40000000; 
            $a = ($a >> ($b - 1)); 
        } else { 
            $a = ($a >> $b); 
        } 
        return $a; 
    }

echo uRShift(10,3)."<br />";
echo uRShift(-10,3)."<br />";
echo uRShift(33,33)."<br />";
echo uRShift(-10,-30)."<br />";
echo uRShift(-14,5)."<br />";
这可能得到同样的结果吗

最接近我想要的功能是:

谢谢您的帮助。

尝试此功能:

function unsigned_shift_right($value, $steps) {
    if ($steps == 0) {
         return $value;
    }

    return ($value >> $steps) & ~(1 << (8 * PHP_INT_SIZE - 1) >> ($steps - 1));
}

最后,我想我找到了解决方案,我不知道为什么,但这段代码对我很有用:

function uRShift($a, $b) 
    { 
        if ($b > 32 || $b < -32) {
            $m = (int)($b/32);
            $b = $b-($m*32);
        }

        if ($b < 0)
            $b = 32 + $b;

        if ($a < 0) 
        { 
            $a = ($a >> 1); 
            $a &= 2147483647; 
            $a |= 0x40000000; 
            $a = ($a >> ($b - 1)); 
        } else { 
            $a = ($a >> $b); 
        } 
        return $a; 
    }
函数uRShift($a,$b)
{ 
如果($b>32 | |$b<-32){
$m=(整数)(b/32美元);
$b=$b-($m*32);
}
如果($b<0)
$b=32+$b;
如果($a<0)
{ 
$a=($a>>1);
$a&=2147483647;
$a |=0x40000000;
$a=($a>>($b-1));
}否则{
$a=($a>>$b);
} 
返回$a;
}

PHP给我的结果:1230584309092136950 0 1073741823 576460752303423487为什么?可能是
function unsigned_shift_right($value, $steps) {
    if ($steps == 0) {
         return $value;
    }

    return ($value >> $steps) & ~(1 << (8 * PHP_INT_SIZE - 1) >> ($steps - 1));
}
1
536870910
16
1073741821
134217727
function uRShift($a, $b) 
    { 
        if ($b > 32 || $b < -32) {
            $m = (int)($b/32);
            $b = $b-($m*32);
        }

        if ($b < 0)
            $b = 32 + $b;

        if ($a < 0) 
        { 
            $a = ($a >> 1); 
            $a &= 2147483647; 
            $a |= 0x40000000; 
            $a = ($a >> ($b - 1)); 
        } else { 
            $a = ($a >> $b); 
        } 
        return $a; 
    }