Rallan,
The code you've got is ALMOST perfect...
But it always fail when $x < 0.
After HOURS studying it and testing it and comparing it to the real results coming from FLASH CS3 (AS3), I did it. I fixed the code!
I know it can (and must) be optimized!
But I can't handle it anymore, at least now. Sooo tired!
Here is the code:
function SHR($x, $n){
$mask = 0x40000000;
if ($x < 0){
$x &= 0x7FFFFFFF;
$mask = $mask >> ($n-1);
$ret = ($x >> $n) | $mask;
// My fix comes here (setting the bit '32' as positive):
$ret = str_pad(decbin($ret), 32, '0', STR_PAD_LEFT);
$ret[0] = '1';
$ret = bindec($ret);
}
else{
$ret = (int)$x >> (int)$n;
}
return $ret;
}
I registered to this forum ONLY to post the solution.
I hope I can help other people having the same headache as me.
Anyway, thanks Rallan! Without you, I would be completely lost by now.
Good luck to everyone.