Behaviour of unsigned right shift applied to byte variable

前端 未结 6 621
星月不相逢
星月不相逢 2020-11-30 07:46

Consider the following snip of java code

byte b=(byte) 0xf1;
byte c=(byte)(b>>4);
byte d=(byte) (b>>>4);

output:

<         


        
6条回答
  •  时光取名叫无心
    2020-11-30 08:19

    I'd guess that b is sign extended to int before shifting.

    So this might work as expected:

    (byte)((0x000000FF & b)>>4)
    

提交回复
热议问题