HelloWorld - 7 months ago 71

Javascript Question

I would like to shift this unsigned number: 1479636484000 7 bits to the right. Is this possible in JavaScript?

Both

`1479636484000 >> 7`

and

`1479636484000 >>> 7`

returns an incorrect answer (for me). The correct answer should be 11559660031.

I guess there's some sign bit involved here, and maybe the number is too large to be supported. But is there any clever way of getting around it?

Answer

Bitwise operations in JavaScript start by truncating the number to a 32-bit integer. Your numbers are too big. The "clever way" to get around that limitation is to implement your own numeric library.

Note that floating-point division by 128 gets you the right answer (if you drop the fraction).

Source (Stackoverflow)