开发者

Bit shift in Java

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


EDIT: Okay, I'm leaving the bottom part for posterity...

If x is int, then it's pretty simple: x << 8 will have the same value as x & 0xff if and only if none of the "middle" 16 bits are set:

  • The top 8 bits of x will be rendered irrelevant by the left shift
  • The bottom 8 bits of x are preserved by the masking

If there are any of the "middle" 16 bits set, then x & 0xff will differ from x 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 is byte, and that it's actually negative.

There's no shift operation defined for byte, so first there's a transformation to int... 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:

11111111111111111111111100000000 = -256

Now consider the second line of code - that's taking x & 0xff, which will only take the bottom 8 bits of the promoted value of x - i.e. 255. You shift that left by 8 bits and you end up with 16 0s, 8 1s, then 8 0s:

00000000000000001111111100000000 = 65280


Below, x is > 0xff , and in one case you mask away the upper 3 bytes, meaning you you do (0x123 & 0x00ff) << 8 , which will be 0x23 << 8 And that is quite different from 0x123 << 8

int x = 0x123;
System.out.println(x << 8);  //prints 74496
System.out.println((x &0x00ff) << 8); //prints 8960

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.

byte x = (byte)0xAA; //x is negative
System.out.println(x << 8);  //prints -22016
System.out.println((x &0x00ff) << 8); //prints 43520
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜