Java没有无符号数,这会带来很多问题。static byte[] x = {(byte) 0xff,(byte) 0xff};
public static void main(String[] args) throws IOException {
byte a = x[0];
int z = a&0xff;
System.out.println(z);
}
int z = a&0xff,
首先a为byte类型,在对其进行&操作时,会先将其转换成int类型,即
1111 1111 1111 1111 1111 1111 1111 1111
&
0000 0000 0000 0000 0000 0000 1111 1111
= 0000 0000 0000 0000 0000 0000 1111 1111
结果为255,若不进行&0xff操作直接赋值给z,则得到的值为-1.
本文原创发布php中文网,转载请注明出处,感谢您的尊重!