Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.3k views
in Technique[技术] by (71.8m points)

bit shift - Bitshift in javascript

I've got a really big number: 5799218898. And want to shift it right to 13 bits.
So, windows-calculator or python gives me:

5799218898 >> 13 | 100010100100001110011111100001 >> 13
70791            | 10001010010000111

As expected.

But Javascript:

5799218898 >> 13 | 100010100100001110011111100001 >> 13
183624           | 101100110101001000

I think it because of internal integer representation in javascript, but cannot find anything about that.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...