Bitshift in javascript

情到浓时终转凉″ 提交于 2019-11-28 00:40:39

In ECMAScript (Javascript) bitwise operations are always in 32-bit. Therefore 5799218898 is chopped into 32-bit which becomes 1504251602. This integer >> 13 gives 183624.

In Python they are arbitrary-length integers. So there's no problem.

(And the numbers in Windows calculator are 64-bit, enough to fit 5799218898.)

(And the correct answer should be 707912.)

The number you have (5799218898) is beyond 32 bits. You didn't mention the JavaScript engine you're testing with, but it's very likely that it is 32-bit.

To test, trim the "5" at the beginning of your number so that you fall inside the 32-bit boundary. Then your shift should work fine.

As Nicholas Zakas states:

Even though JavaScript numbers are technically stored in 64-bits, integer values are treated as if they’re 32 bits whenever bitwise operators are involved.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!