1

Take the following code snippet:

<script>
var x = 25;
document.write(x << 9);
</script>

<?php
$x = 25;
echo ($x << 9);
?>

This outputs: 12800 12800
OK. Normal so far... Now lets try this:

<script>
var x = 12345678;
document.write(x << 9);
</script>

<?php
$x = 12345678;
echo ($x << 9);
?>

This time the output is 2026019840 6320987136

Why are the latter two values different? And, most importantly (to me), how do I get the PHP implementation to do what the Javascript implementation does? In other words, I want my PHP code to output 2026019840 instead of 6320987136

2
  • 3
    they're different because PHP is automatically converting to a long, as Python does. javascript instead gives you the low 32 bits. Commented Mar 24, 2011 at 4:59
  • splendid! So if I just & the value with 0xFFFFFFFF I get the desired value in PHP. Could you please submit that as an answer so I can accept? Commented Mar 24, 2011 at 5:00

2 Answers 2

3

use (x << 9) % 0x100000000 or its PHP equivalent. (or what you just said)

Sign up to request clarification or add additional context in comments.

Comments

1

PHP is giving you a 64 bit result. Javascript is just giving you the lower 32bits. If you use (x << 9) & 0xFFFFFFFF in PHP you'll get the low 32 bits.

you may run into problems with the sign bit though. Try (23456789 << 9)

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.