01001001 的算术左移是什么?

发布于 2024-12-26 11:10:24 字数 84 浏览 2 评论 0原文

我认为是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

魂牵梦绕锁你心扉 2025-01-02 11:10:24

对于左移,算术移位和逻辑移位是相同的。

区别仅在于右移,其中算术右移将在移位后将旧的 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.

柒七 2025-01-02 11:10:24

在 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:

...Note that arithmetic left shift may cause an overflow; this is the only way it differs from logical left shift.

(This is not the case in Java, but just to keep in mind.)

Happy coding.

可遇━不可求 2025-01-02 11:10:24

是的,这是正确的。

x 算术左移 n 位等于 x * (2^n)。因此,在您的示例中,01001001 的 ar 左移 1 位置等于 10010010 (73 * 21 = 146)。

Yes it is correct.

The arithmetic left shift of x by n places is equal to x * (2^n). So in your example is the ar-left-shift of 01001001 by 1 place equal to 10010010 (73 * 2¹ = 146).

路还长,别太狂 2025-01-02 11:10:24

当您左移 1 位位置时,您是正确的。它等于 10010010。

当您按如下方式向左移动 4 位时,您会得到以下答案。

01001001 << 4 = 10010000

当您如下右移 4 位时,您将得到以下答案。

01001001 >> 4 = 00000100

由于移位而留下的空位将用零填充。

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.

01001001 << 4 = 10010000

when you shift 4 bits to the right as follows, you get the following answer.

01001001 >> 4 = 00000100

Bits that are left empty as a result of shifting are filled with zeros.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文