Java 为什么字节 b = (byte) 0xFF 等于整数 -1?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1677957/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why byte b = (byte) 0xFF is equals to integer -1?
提问by okami
Why byte b = (byte) 0xFF
is equal to integer
-1
?
为什么byte b = (byte) 0xFF
等于integer
-1
?
Ex:
前任:
int value = byte b = (byte) 0xFF;
System.out.println(value);
it will print -1
?
它会打印-1
吗?
回答by James Black
If you are using a signed int then 0xFF = -1 due to the 2-complement.
如果您使用的是有符号整数,则由于 2 补码,因此 0xFF = -1。
This wiki article explains it well, see the table on the right: http://en.wikipedia.org/wiki/Two%27s_complement
这篇维基文章解释得很好,见右表:http: //en.wikipedia.org/wiki/Two%27s_complement
回答by Michael Petrotta
Because Java (and most languages) represent negative integer values using two's-complement math. In two's-complement, 0xFF (11111111) represents (in a signed int) the value -1.
因为 Java(和大多数语言)使用二进制补码 math表示负整数值。在二进制补码中,0xFF (11111111) 表示(在有符号整数中)值 -1。
回答by cletus
Bytes are signed in Java. In binary 0x00 is 0, 0x01 is 1 and so on but all 1s (ie 0xFF) is -1, 0xFE is -2 and so on. See Two's complement, which is the binary encoding mechanism used.
字节用 Java 签名。在二进制中 0x00 是 0,0x01 是 1 等等,但所有的 1(即 0xFF)是 -1,0xFE 是 -2 等等。请参阅Two's补码,这是使用的二进制编码机制。
回答by jason
b
is promoted to anint
in determining which overload ofsystem.out.println
to call.All bytes in Java are signed.
The signed byte
0xff
represents the value-1
. This is because Java uses two's complementto represent signed values. The signed byte0xff
represents-1
because its most significant bit is1
(so therefore it represents a negative value) and its value is-128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = -1
.
b
int
在确定system.out.println
调用哪个重载时被提升为。Java 中的所有字节都是有符号的。
有符号字节
0xff
表示值-1
。这是因为 Java 使用二进制补码来表示有符号值。有符号字节0xff
表示,-1
因为它的最高有效位是1
(因此它表示负值)并且它的值为-128 + 64 + 32 + 16 + 8 + 4 + 2 + 1 = -1
。
回答by SDGator
Its not just Java that does 2's complement math. That is the way every microprocessor and DSP that I can think of does math. So, its the way every programming language represents it.
它不仅仅是 Java 做 2 的补码数学。这就是我能想到的每个微处理器和 DSP 进行数学运算的方式。因此,它是每种编程语言表示它的方式。
回答by SingleNegationElimination
perhaps your confusion comes from why (byte)0xFF
is somehow equal to (int)0xFFFFFFFF
. What's happening here is the promotion from smaller to larger signed types causes the smaller value to be sign extended, whereby the most significant bit is copied to all of the new bits of the promoted value. an unsigned type will not become sign-extended, they get zero extended, the new bits will always be zero.
也许您的困惑来自于为什么(byte)0xFF
以某种方式等于(int)0xFFFFFFFF
. 这里发生的事情是从较小到较大的有符号类型的提升导致较小的值被符号扩展,由此最高有效位被复制到提升值的所有新位。无符号类型不会被符号扩展,它们会被零扩展,新位将始终为零。
If it helps you to swallow it, think of it this way, every integer of any size also has some 'phantom' bits that are too significant to be represented. they are there, just not stored in the variable. a negative number has those bits nonzero, and positive numbers have all zeros for phantom bits when you promote a smaller value to a larger one, those phantom bits become real bits.
如果它可以帮助您吞下它,请这样想,任何大小的每个整数也有一些太重要而无法表示的“幻影”位。它们在那里,只是没有存储在变量中。当您将较小的值提升为较大的值时,负数的那些位非零,而正数的幻位位全为零,这些幻位位成为真实位。
回答by Gagandeep Singh
reduced modulo
减模
byte = 256 0xff = 255
字节 = 256 0xff = 255
255 / 256 -> remainder 255
255 / 256 -> 余数 255
So 255 - 256 = -1
所以 255 - 256 = -1
Simple Logic Cheers
简单的逻辑干杯