Difference in the results of operations bitwise between javascript and php

前端 未结 2 1751
时光取名叫无心
时光取名叫无心 2021-01-23 22:53

Javascript code:

console.log( 1 << 5 );
console.log( 1111044149 << 2 );

Javascript output:

32
149209300
         


        
2条回答
  •  猫巷女王i
    2021-01-23 23:16

    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
    

    And in PHP, the bits do not get truncated because it is not treated as a 32-bit integer.

提交回复
热议问题