How is left shift of numbers greater than 32 bits?

As I understand it, JS treats numbers as 32 bits when performing bit-shift operations, even though it supports 64-bit numbers.

How can I implement a function leftShiftthat works on 64-bit numbers? those. it will not turn into negatives for something like 192 << 24(it should be 3221225472, not -1073741824).

+4
source share
1 answer

Just do what math left shift :

() (), (, 2 ).

function shift(number, shift) {
    return number * Math.pow(2, shift);
}

shift(192, 24)
//3221225472

shift(3221225472, -24)
//192
+5

Source: https://habr.com/ru/post/1611690/


All Articles