Difference in the results of operations bitwise between javascript and php

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

Javascript code:

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

Javascript output:

32
149209300
         


        
2条回答
  •  一向
    一向 (楼主)
    2021-01-23 23:19

    Thanks for the explanations, helped me a lot and I could now make a function to handle this automatically conversion, I had forgotten that detail about 32-bit and 64-bit for lack of attention.

    Function

    function shift_left_32( $a, $b ) {
        return ( $c = $a << $b ) && $c >= 4294967296 ? $c - 4294967296 : $c;
    }
    

    Test

    var_dump( shift_left_32( 1, 5 ) );
    var_dump( shift_left_32( 1111044149, 2 ) );
    

    Output

    32
    149209300
    

提交回复
热议问题