javascript和php之间按位操作结果的差异 [英] Difference in the results of operations bitwise between javascript and php
问题描述
Javascript代码:
Javascript code:
console.log( 1 << 5 );
console.log( 1111044149 << 2 );
Javascript输出:
Javascript output:
32
149209300
PHP代码:
var_dump( 1 << 5 );
var_dump( 1111044149 << 2 );
PHP输出:
32
4444176596
为何 1111044149< ;< 2
PHP和javascript有区别吗?我正在为PHP编写一个javascript代码,一切都不那么重要,我需要PHP代码显示在javascript中获得的类似结果是完美的。
Why 1111044149 << 2
has difference between PHP and javascript? I'm writing a javascript code to PHP and everything worked less that part and I need the PHP code shows similar results obtained in javascript to be perfect.
推荐答案
JavaScript中按位运算的操作数总是被视为int32,而在PHP中则不是这样。因此,当在 1111044149
上执行左移时,这发生在JS中:
The operands of a bitwise operation in JavaScript are always treated as int32, and in PHP this is not the case. So, when performing the left shift on 1111044149
, this happens in JS:
01000010001110010011000000110101 (original, 32-bit)
00001000111001001100000011010100 (left shifted 2 positions, "01" is truncated)
= 149209300
在PHP中,这些位不会被截断,因为它不被视为32位整数。
And in PHP, the bits do not get truncated because it is not treated as a 32-bit integer.
这篇关于javascript和php之间按位操作结果的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!