为什么将 3 右算术移位到二进制序列“0110 0100”会导致“1110 1100”而不是“0000 1100”?
Why does a right arithmetic shift of 3 to the binary sequence "0110 0100" lead to "1110 1100" instead of "0000 1100"?
在我正在看的一本教科书中,它要求我对二进制序列 0110 0100
进行算术右移 3 (a >> 3
)。我认为它是 0000 1100
,因为考虑到最高有效位是 0
(或者是 01
?),我会在右边添加 3 个零,但答案键表示正确答案是1110 1100
。
算术移位定义如下:
An arithmetic right shift fills the left end with k repetitions of the most significant bit...
由于 0110 0100
中的最高位是 0
,我是否应该添加 0 而不是 1?最高有效位实际上是 01
吗?
作为一个单独的例子,如果一个位指的是前两位而不仅仅是第一个,为什么右移3到二进制序列0111 0010
,0000 1110
,而不是1110 1110
?
Index : 76543210
a >> 0: 01100100
a >> 1: 00110010
a >> 1: 00011001
a >> 1: 00001100
另一个例子:
Index : 76543210
a >> 0: 10000100
a >> 1: 11000010
a >> 1: 11100001
a >> 1: 11110000
正如您所相信的那样。算术移位,也称为带符号移位,已经很好地提示了它的作用,将位左移或右移,同时保留带符号的位,向右移动时复制带符号的位。
正式定义如下:
A shift, applied to the representation of a number in a fixed radix
numeration system and in a fixed-point representation system, and in
which only the characters representing the fixed-point part of the
number are moved. An arithmetic shift is usually equivalent to
multiplying the number by a positive or a negative integral power of
the radix, except for the effect of any rounding; compare the logical
shift with the arithmetic shift, especially in the case of
floating-point representation.
维基百科条目有一些很好的说明性图片:https://en.wikipedia.org/wiki/Arithmetic_shift
可能作者只是搞砸了他自己的例子,想证明它保留了 left-most 位(在大多数情况下)并复制了它。虽然他忘记设置了。
在我正在看的一本教科书中,它要求我对二进制序列 0110 0100
进行算术右移 3 (a >> 3
)。我认为它是 0000 1100
,因为考虑到最高有效位是 0
(或者是 01
?),我会在右边添加 3 个零,但答案键表示正确答案是1110 1100
。
算术移位定义如下:
An arithmetic right shift fills the left end with k repetitions of the most significant bit...
由于 0110 0100
中的最高位是 0
,我是否应该添加 0 而不是 1?最高有效位实际上是 01
吗?
作为一个单独的例子,如果一个位指的是前两位而不仅仅是第一个,为什么右移3到二进制序列0111 0010
,0000 1110
,而不是1110 1110
?
Index : 76543210
a >> 0: 01100100
a >> 1: 00110010
a >> 1: 00011001
a >> 1: 00001100
另一个例子:
Index : 76543210
a >> 0: 10000100
a >> 1: 11000010
a >> 1: 11100001
a >> 1: 11110000
正如您所相信的那样。算术移位,也称为带符号移位,已经很好地提示了它的作用,将位左移或右移,同时保留带符号的位,向右移动时复制带符号的位。
正式定义如下:
A shift, applied to the representation of a number in a fixed radix numeration system and in a fixed-point representation system, and in which only the characters representing the fixed-point part of the number are moved. An arithmetic shift is usually equivalent to multiplying the number by a positive or a negative integral power of the radix, except for the effect of any rounding; compare the logical shift with the arithmetic shift, especially in the case of floating-point representation.
维基百科条目有一些很好的说明性图片:https://en.wikipedia.org/wiki/Arithmetic_shift
可能作者只是搞砸了他自己的例子,想证明它保留了 left-most 位(在大多数情况下)并复制了它。虽然他忘记设置了。