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.

Jon Seigel
  • 12,251
  • 8
  • 58
  • 92
Andrew
  • 8,330
  • 11
  • 45
  • 78
  • 3
    The right answer is actually 707912 (10101100110101001000) which makes it clearer that the first two binary digits are getting chopped off. – Miles Mar 03 '10 at 18:22
  • 1
    [BigInts](https://v8.dev/features/bigint) will help you `console.log((1n << 32n).toString())`, but the support is not quite there .. – Jonas Wilms Jun 21 '19 at 11:21

4 Answers4

33

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

kennytm
  • 510,854
  • 105
  • 1,084
  • 1,005
  • Thanks, that's it. Looks like I can emulate this behavior in python like this (5799218898 & 0xFFFFFFFF) >> 13 – Andrew Mar 03 '10 at 18:28
6

If you have a modern browser, you might want to use bigint for values greater than 32-bit signed. They were introduced in the 11-th Edition of ECMAScript Language back in 2020.

It is stated, you also find a browser compatibility table:

Bitwise operators are supported as well, except >>> (zero-fill right shift), as every BigInt value is signed. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/BigInt

You can do the following, tested for example in Chrome:

> Number(BigInt(5799218898) >> BigInt(13))
<- 707912
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.

Marcel Korpel
  • 21,536
  • 6
  • 60
  • 80
2

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.

Ates Goral
  • 137,716
  • 26
  • 137
  • 190
  • 1
    I'm using Firefox 3.6 on 64bit system – Andrew Mar 03 '10 at 18:22
  • 8
    All JavaScript engines use 64-bit floating point numbers, and convert to 32-bit integers for bitwise operations, regardless of the architecture. It's defined in the ECMAScript standard. – Matthew Crumley Mar 03 '10 at 20:18