Alex Rivera | Logout

Why do bit shifts of large numbers give incorrect results in JavaScript?

Asked 2010-03-03T18:13:43.710
29

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.

Edit
Report

1 Answer

4

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.

answered 2010-03-03T18:23:11.447

Your Answer