移位運算符操作數是負數的處理方式

i<<-1和i<<31 的結果一樣
1 “-1”表示成補碼是1111 ….11 1111 ,31是 0000 …0001 1111,,他們的後五位是一樣的。
2 Interger的移位運算只注意後5位,看註釋
Note also that rotation by any multiple of 32 is a no-op, so all but the last five bits of the rotation distance can be ignored, even if the distance is negative:
總結:將負數寫爲補碼,這個數的低5位便是其真正移位的數字

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章