Why does bit-wise shift left return different results in Python and Java?

前端 未结 3 1949
遇见更好的自我
遇见更好的自我 2021-02-19 14:05

I\'m trying to port some functionality from a Java app to Python.

In Java,

System.out.println(155 << 24);

Returns: -1694498816

3条回答
  •  [愿得一人]
    2021-02-19 14:31

    Use long in java to get the same result

    System.out.println(155L << 24);
    

    instead of

    System.out.println(155 << 24);
    

    Long are 4 byte length so the precision is the same for this context to python integers.

提交回复
热议问题