【发布时间】:2018-08-15 11:47:22
【问题描述】:
我知道byte 和short 数据类型在Java 中没有给予2's complement 处理。我的这个陈述的意思在下面的例子中得到了证明:
byte b1 = 0xFF; //error, 255 is outside the range of byte
byte b2 = 0b1100; //12 is stored, and not -4
现在,举个例子:
byte b3 = 0;
b3 += 0xFF;
System.out.println(b3); //prints -1 on the console
上面例子中的第2行是否应该抛出错误,单个0xFF是255并且在byte范围之外?
另外,byte 和 short 是什么不公平的游戏?我的意思是,为什么不使用2's complement system 来解释它们,就像int 和long 一样?
【问题讨论】:
-
没有给出 2 的补码 哈?你知道吗?听说过溢出吗?
-
为什么
b2会是-4?你认为一个字节是多少位? -
哦,哦,我错了。它读作 0000_1100,不是吗?因此,它存储了 12 个。该死的,多么幼稚。