算数位移和逻辑位移

逻辑位移是移动数字的全部物理比特位。java

算数位移是对数字除了符号位之外的比特位进行位移,符号位保持不变。this

当具体到以二进制补码表示的负数的方式上,算数位移的表现形式就是在最高位补1.
固然你尚未学到二进制补码。立刻就快了。code

若是负数的表示形式不是二进制补码(这不多见),那算数位移就不必定是在最高位补1.orm

在汇编层面上,算数位移和逻辑位移是两条不一样的指令。
在java语言中,使用>>>表示逻辑位移,使用>>表示算数位移,因此即便是有符号数,也能够作逻辑位移
在C语言中,只有>>表示位移。对于有符号数,一般(但不是绝对)使用的是算数位移。ci

“无符号数就是逻辑,有符号数就是算数”,这个推论是不严谨的。虽然绝大多数状况下是正确的。it

“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."io

CSAPP 2.1.10里面清楚的写了。form

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

看完书,不等于看懂书。一本CSAPP你能半年学透了都是很不错的成绩了。object

相关文章
相关标签/搜索