本文介绍了如何在PHP中执行适当的无符号右移?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
是否有可能在PHP和Javascript中获得相同的结果?
Is this possible to get the same results in PHP and 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>
输出:
1
536870910
16
1073741821
134217727
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 />";
输出:
1
536870910
0
0
134217727
是否有可能获得相同的结果?
Is this possible to get the same results?
最接近我想要的功能在这里:
Closest function to what I want is here:
推荐答案
function unsigned_shift_right($value, $steps) {
if ($steps == 0) {
return $value;
}
return ($value >> $steps) & ~(1 << (8 * PHP_INT_SIZE - 1) >> ($steps - 1));
}
The output, based on your code sample:
1
536870910
16
1073741821
134217727
这篇关于如何在PHP中执行适当的无符号右移?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!