【发布时间】:2013-04-27 12:26:32
【问题描述】:
我在搞乱按位运算符,我试图将一个负字节转换为一个无符号的 8 位值,这就是人们的建议:
System.out.println(-20 & 0xFF); //bitwise AND on negative number and 255
所以,这完美运行,并返回 236,但为什么呢?就我而言:
00010100 //binary representation of -20
11111111 //binary representation of 0xFF or 255
--------
00010100 //it returns the same exact thing, so it's either -20 or 20
为什么有效?我想我错过了一些非常简单的东西,但我似乎无法掌握它。
此外,如果我使用低于 256 的正数执行此操作,它会返回相同的数字。我似乎无法理解 Java 对这些数字的作用。
【问题讨论】:
-
-20 != 20 所以表示也不一样。
标签: java char byte bit-manipulation bit