Why does 0x80000000 >> 1 in JavaScript produce a negative value?

后端 未结 2 1561
日久生厌
日久生厌 2021-01-18 17:45

Doing some tests with bitwise operations / shifting with JavaScript

0x80000000 >> 1 // returns -1073741824 (-0x40000000)

I would expe

2条回答
  •  孤城傲影
    2021-01-18 18:18

    Its an arithmetic shift that's why the sign is preserved, to do a logical shift use >>>

    0x80000000 >>> 1 // returns 1073741824 (0x40000000)
    

提交回复
热议问题