2

In Javascript

p = 105874240;
105874240
p << 5;
-906991616

But in PHP

php > echo $p = 105874240;
105874240
php > echo $p << 5;
3387975680

I search some introduce about the different, because Javascript use signed int32. But I can't find the right solution for it, all not work for me.

Could you please tell me what's going on, and how can I get the same result of Javascript in PHP. Thank you.

3
  • Take a look at this stackoverflow.com/questions/1908492/… you need to use the >>> operator to convert to 32bit unsigned int in js. Commented Feb 11, 2017 at 8:55
  • I mean Javascript result is right, I want PHP get the same result, not change the Javascipt. Commented Feb 11, 2017 at 8:58
  • Do you know the difference(s) between signed and unsigned integers and how to tell the OS which one to use and when? Commented Feb 11, 2017 at 10:11

1 Answer 1

2

This is how I'd do it.

function to32bits($value)
{
    $value = ($value & 0xffffffff);
    if ($value & 0x80000000) $value = -((~$value & 0xffffffff) + 1);
    return $value;
}

$p = 105874240;
echo to32bits($p << 5); // -906991616
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.