Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I perform a proper unsigned right shift in PHP?

Is this possible to get the same results in PHP and Javascript?

Example:

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>

output:

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 />";

output:

1
536870910
0
0
134217727

Is this possible to get the same results?

Closest function to what I want is here:

Unsigned right shift function not working for negative input

Thanks for help.

like image 582
Kamil Wiśniewski Avatar asked Oct 19 '25 05:10

Kamil Wiśniewski


2 Answers

Try this function:

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
like image 126
silkfire Avatar answered Oct 21 '25 19:10

silkfire


Finally I think I found solution I don't know why but this code works for me:

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; 
    }
like image 29
Kamil Wiśniewski Avatar answered Oct 21 '25 20:10

Kamil Wiśniewski



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!