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:
function zeroFill($a, $b)
{
$z = hexdec(80000000);
if ($z & $a)
{
$a = ($a>>1);
$a &= (~$z);
$a |= 0x40000000;
$a = ($a>>($b-1));
}
else
{
$a = ($a>>$b);
}
return $a;
}
but unfortunately, it doesn't work perfectly.
EG: -1149025787 >>> 0 Javascript returns 3145941509 PHP zeroFill() return 0
Twice as fast for negative numbers as using the decimal-binary conversions
I studied around the webs and come out with my own zerofill function, base on the explanation given. This method works for my program.
Have a look:
Not sure if this works for php, I've manage to get it to work with C#.
I found that out by debugging into the code that uses
>>>
comparing it with C# version of the code I converted from javascript. While trying out withb = 0
, and using a scientific calculator to see the different hex/dec result of>>
and>>>
produce by javascript. Whena
is negative,>>>
actually makes aa as unsigned.Not sure if that works for all scenario, but for my case the
>>>
is for md5 hashing. Being able to produce similar output, I'm quite satisfied with the result.Hope that helps
Enjoy it.
Your function doesn't work because when
$b == 0
, the expressionwill be evaluated, which returns 0.
Assuming 32-bit machines, you can add a special case:
This works for me