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
use (x << 9) % 0x100000000
or its PHP equivalent. (or what you just said)
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.