Is byte not signed two's complement?

我怕爱的太早我们不能终老 提交于 2020-01-05 03:30:28

问题


Given that byte,short and int are signed, why do byte and short in Java not get the usual signed two's complement treatment ? For instance 0xff is illegal for byte.

This has been discussed before here but I couldn't find a reason for why this is the case.


回答1:


If you look at the actual memory used to store -1 in signed byte, then you will see that it is 0xff. However, in the language itself, rather than the binary representation, 0xff is simply out of range for a byte. The binary representation of -1 will indeed use two's complement but you are shielded from that implementation detail.

The language designers simply took the stance that trying to store 255 in a data type that can only hold -128 to 127 should be considered an error.

You ask in comments why Java allows:

int i = 0xffffffff;

The literal 0xffffffff is an int literal and is interpreted using two's complement. The reason that you cannot do anything similar for a byte is that the language does not provide syntax for specifying that a literal is of type byte, or indeed short.

I don't know why the decision not to offer more literal types was made. I expect it was made for reasons of simplicity. One of the goals of the language was to avoid unnecessary complexity.




回答2:


You can write

int i = 0xFFFFFFFF;

but you can't write

byte b = 0xFF;

as the 0xFF is an int value not a byte so its equal to 255. There is no way to define a byte or short literal, so you have to cast it.

BTW You can do

byte b = 0;
b += 0xFF;
b ^= 0xFF;

even

byte b = 30;
b *= 1.75; // b = 52.



回答3:


it is legal, but you need to cast it to byte explicitly, i.e. (byte)0xff because it is out of range.




回答4:


You can literally set a byte, but surprisingly, you have to use more digits:

byte bad = 0xff; // doesn't work
byte b = 0xffffffff; // fine

The logic is, that 0xff is implicitly 0x000000ff, which exceeds the range of a byte. (255)

It's not the first idea you get, but it has some logic. The longer number is a smaller number (and smaller absolute value).

byte b = 0xffffffff; // -1 
byte c = 0xffffff81; // -127 
byte c = 0xffffff80; // -128



回答5:


The possible values for a byte range from -128 to 127.

It would be possible to let values outside the range be assigned to the variable, and silently throw away the overflow, but that would rather be confusing than conventient.

Then we would have:

byte b = 128;
if (b < 0) {
  // yes, the value magically changed from 128 to -128...
}

In most situations it's better to have the compiler tell you that the value is outside the range than to "fix" it like that.



来源:https://stackoverflow.com/questions/10430820/is-byte-not-signed-twos-complement

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!