日期:2014-05-20 浏览次数:20741 次
0xFF is hexidecimal, you can Wikipedia that part. FF is a representation of 00000000 00000000 00000000 11111111 (a 32-bit integer) & means bit-wise "and", and so when you use it on two ints, each pair of bits from those two ints is and-ed and the result is placed in the resultant int: Example (showing 16 bits only) 0101 1100 1010 1100 &0000 0000 1111 1111 ----------------- =0000 0000 1010 1100 So as you can see, 0xFF is often used to to isolate a byte in an integer, by truncating the integer to one byte's worth of 1's. For more information, see this WP article:
------解决方案--------------------
与 0xff 做 & 运算会将 byte 值变成 int 类型的值,也将 -128~0 间的负值都转成正值了。
------解决方案--------------------
char c = (char)-1 & 0xFF; char d = (char)-1; System.out.println((int)c); System.out.println((int)d);
------解决方案--------------------
1) 位运算不限制为int, long也行的。
2)
3) 负数进行&操作时需要转成补码,-2 的补码是0xFFFFFFFE