2016-06-25 231 views
-4

我收到这样的问题:铸造一个字节十六进制

enter image description here

答案是ffffff80, 但我不明白他们是如何得到它.. 为什么铸造整数调用2的补充?

+0

你是什么意思的答案?代码打印3行。 – user2259824

+0

CPU使用2的补码进行计算,Java只是提供更友好的方式来使用这些计算。 –

回答

1
a: 0x11 -> 17(dec) -> 10001 -> bitshift by 7 -> 100010000000 -> 0x880 -> 2176(dec) 
b: (byte) a -> 10000000 -> 0x80 -> 128(dec) 
c: (char) b -> char is signed, byte is unsigned -> 0x80 -> -128(dec)