Java 中的位移位
可能是我太累了。
为什么以下不显示相同的值?
int x = 42405;
System.out.println(x << 8);
System.out.println((x &0x00ff) << 8);
在这两种情况下,较低位都应该是清楚的
May be I am too tired.
Why dont't the following display the same value?
int x = 42405;
System.out.println(x << 8);
System.out.println((x &0x00ff) << 8);
The lower bits should be clear in both cases
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
编辑:好的,我将底部部分留给后人...
如果
x
是int
,那么它非常简单:x << 8
的值与x & 相同。 0xff
当且仅当“中间”16 位均未设置时:x
的前 8 位将因左移而变得无关x 的底 8 位
被掩码保留如果有任何“中间”16 位设置,则
x & 被保留。 0xff
与x
有一点不同,该位仍被移位保留,因此结果会不同。我不明白为什么您希望它们总是给出相同的结果...
我将假设
x
的类型是byte
,并且它实际上是负面的。没有为
byte
定义移位操作,因此首先会转换为int
...这取决于您是否拥有掩码。因此,我们以
x
为 -1 的情况为例。那么(int) x
也是 -1 - 即位模式全为1。将其左移 8 位,最终得到 24 个 1 后跟 8 个 0 的位模式:现在考虑第二行代码 - 即采用 x & 。 0xff,它只会采用
x
提升值的底部 8 位 - 即 255。将其左移 8 位,最终得到 16 个 0、8 个 1,然后8 个 0:EDIT: Okay, I'm leaving the bottom part for posterity...
If
x
isint
, then it's pretty simple:x << 8
will have the same value asx & 0xff
if and only if none of the "middle" 16 bits are set:x
will be rendered irrelevant by the left shiftx
are preserved by the maskingIf there are any of the "middle" 16 bits set, then
x & 0xff
will differ fromx
in a bit which is still preserved by the shift, therefore the results will be different.I don't understand why you'd expect them to always give the same results...
I'm going to assume that the type of
x
isbyte
, and that it's actually negative.There's no shift operation defined for
byte
, so first there's a transformation toint
... and that's what can change depending on whether or not you've got the mask.So let's take the case where
x
is -1. Then(int) x
is also -1 - i.e. the bit pattern is all 1s. Shift that left by 8 bits and you end up with a bit pattern of 24 1s followed by 8 0s:Now consider the second line of code - that's taking
x & 0xff
, which will only take the bottom 8 bits of the promoted value ofx
- i.e. 255. You shift that left by 8 bits and you end up with 16 0s, 8 1s, then 8 0s:下面,x是> 0xff ,在一种情况下,您屏蔽了高 3 个字节,这意味着您
你做
(0x123 & 0x00ff) << 8
,这将是0x23 << 8
这与0x123 << 完全不同。 8
如果 x 是一个字节,它会在移位之前“提升”为 int,如果它是负数,它会符号扩展,并且你会在整数中得到很多 1 位,这在一种情况下被 &0xff 屏蔽掉,而在另一种情况下则未被屏蔽掉,
即。
Below, x is > 0xff , and in one case you mask away the upper 3 bytes, meaning you
you do
(0x123 & 0x00ff) << 8
, which will be0x23 << 8
And that is quite different from0x123 << 8
And if x is a byte, it will get "promoted" to int before the shift, and if it's negative, it'll sign extend, and you get a whole lot of 1 bits set in the integer, which is masked away with &0xff in one case, and not masked away in the other
i.e.