01001001 的算术左移是什么?
我认为是00010010 即它试图保持符号位不变。
另一方面,逻辑左移 1 个位置将是 10010010
这是正确的吗?
I would think it is 00010010
i.e. it tries to maintain the sign bit as is
On the other hand, the logical left shift by 1 pos would be
10010010
Is this correct?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
对于左移,算术移位和逻辑移位是相同的。
区别仅在于右移,其中算术右移将在移位后将旧的 MSB 复制到新的 MSB,从而在移位时防止负数转换为正数。
维基百科有更详细的解释。
For left shift, arithmetic and logical shift are the same.
The difference is for right shift only, where an arithmetic right shift will copy the old MSB to the new MSB after having shifted, thus keeping a negative number from being converted to a positive when shifting.
Wikipedia has a more detailed explanation.
在 Java 中
<<
是逻辑左移。始终添加 0 作为 LSB。(请注意,Java 将提升有问题的[字节]值,因此必须小心掩码回为八位字节!否则您将保留移位的位( s),其中可能包含“1”。)
但是,维基百科关于算术移位的文章表明那算术左移可能导致溢出错误:
(Java 中的情况并非如此,但请记住。)
祝您编码愉快。
In Java
<<
is a logical left-shift. 0 is always added as the LSB.(Do note that Java will promote the [byte] value in question, so care must be taken to mask back to an octect! Otherwise you'll keep the shifted bit(s), which might have included "1".)
However, the Wikipedia article on Arithmetic shift indiciates that an arithmetic left shift may result in an overflow error:
(This is not the case in Java, but just to keep in mind.)
Happy coding.
是的,这是正确的。
x
算术左移n
位等于x * (2^n)
。因此,在您的示例中,01001001
的 ar 左移1
位置等于10010010
(73 * 21 = 146)。Yes it is correct.
The arithmetic left shift of
x
byn
places is equal tox * (2^n)
. So in your example is the ar-left-shift of01001001
by1
place equal to10010010
(73 * 2¹ = 146).当您左移 1 位位置时,您是正确的。它等于 10010010。
当您按如下方式向左移动 4 位时,您会得到以下答案。
当您如下右移 4 位时,您将得到以下答案。
由于移位而留下的空位将用零填充。
You are correct when you left shift by 1 bit postion. It equals 10010010.
when you shift 4 bits to the left as follows, you get the following answer.
when you shift 4 bits to the right as follows, you get the following answer.
Bits that are left empty as a result of shifting are filled with zeros.