展开全部
public class Test {
public static void main(String[] args) {
System.out.println(~2);
System.out.println(byteToBit((byte) 2));
System.out.println(byteToBit((byte) ~2));
}
public static String byteToBit(byte b) {
return ""
+ (byte) ((b >> 7) & 0x1) + (byte) ((b >> 6) & 0x1)
+ (byte) ((b >> 5) & 0x1) + (byte) ((b >> 4) & 0x1)
+ (byte) ((b >> 3) & 0x1) + (byte) ((b >> 2) & 0x1)
+ (byte) ((b >> 1) & 0x1) + (byte) ((b >> 0) & 0x1);
}
}
~2 = -3。
2的bit值为00000010,~表示按位e69da5e887aa3231313335323631343130323136353331333363386664取反,11111101转换为十进制数字后是-3