文章出處

邏輯位移是移動數字的所有物理比特位。

算數位移是對數字除了符號位以外的比特位進行位移,符號位保持不變。

當具體到以二進制補碼表示的負數的方式上,算數位移的表現形式就是在最高位補1.
當然你還沒有學到二進制補碼。馬上就快了。

如果負數的表示形式不是二進制補碼(這很少見),那算數位移就不一定是在最高位補1.

在匯編層面上,算數位移和邏輯位移是兩條不同的指令。
在java語言中,使用>>>表示邏輯位移,使用>>表示算數位移,所以即使是有符號數,也可以做邏輯位移
在C語言中,只有>>表示位移。對于有符號數,通常(但不是絕對)使用的是算數位移。

“無符號數就是邏輯,有符號數就是算數”,這個推論是不嚴謹的。雖然絕大多數情況下是正確的。

“The C standard does not precisely define which type of right shift should be used. For unsigned data (i.e.,
integral objects declared with the qualifier unsigned), right shifts must be logical. For signed data (the
default), either arithmetic or logical shifts may be used. This unfortunately means that any code assuming
one form or the other will potentially encounter portability problems. In practice, however, almost all
compiler/machine combinations use arithmetic right shifts for signed data, and many programmers assume
this to be the case."

CSAPP 2.1.10里面清楚的寫了。

For signed data (the default), either arithmetic or logical shifts may be used.

看完書,不等于看懂書。一本CSAPP你能半年學透了都是很不錯的成績了。


文章列表


不含病毒。www.avast.com
arrow
arrow
    全站熱搜
    創作者介紹
    創作者 大師兄 的頭像
    大師兄

    IT工程師數位筆記本

    大師兄 發表在 痞客邦 留言(0) 人氣()