Bitwise operations with big integers

Viewed 3541

I am implementing decoding of BER-compressed integers and recently I've found a weird JavaScript behavior related to bitwise operations with big integers.

E.g.:

var a = 17516032;          // has 25 bits
alert(a << 7)              // outputs -2052915200
alert(a * 128)             // outputs  2242052096
alert(2242052096 >> 16)    // outputs -31325
alert(2242052096 / 65536)  // outputs  34211

While the first workaround (multiplication instead of left shift) is acceptable, the second isn't.

Why it happens? How to bear with it?

4 Answers
Related