Java 二进制文字 - 字节值 -128

2022-01-09 00:00:00 binary byte literals java twos-complement

由于 SE 7 Java 允许将值指定为二进制文字.文档告诉我字节"是一种可以容纳 8 位信息的类型,值 -128 到 127.

Since SE 7 Java allows to specify values as binary literal. The documentation tells me 'byte' is a type that can hold 8 Bit of information, the values -128 to 127.

现在我不知道为什么,但如果我尝试将二进制文字分配给 Java 中的一个字节,我无法定义 8 位,而只能定义 7 位,如下所示:

Now i dont know why but i cannot define 8 bits but only 7 if i try to assign a binary literal to a byte in Java as follows:

byte b = 0b000_0000;    //solves to the value 0
byte b1 = 0b000_0001;   //solves to the value 1
byte b3 = 0b000_0010;   //solves to the value 2
byte b4 = 0b000_0011;   //solves to the value 3     

以此类推,直到我们使用这 7 位得到最后几种可能性:

And so on till we get to the last few possibilitys using those 7 bits:

byte b5 = 0b011_1111;   //solves to the value 63
byte b6 = 0b111_1111;   //solves to the value 127

如果我想让它变成负数,我必须在前面添加一个前导 - 像这样:

If i want to make it negative numbers i have to add a leading - in front like this:

byte b7 = -0b111_1111;   //solves to the value -127

现在我遇到的一半问题是我只使用 7 位来描述他们告诉我的是 8 位数据类型.下半部分是,除非使用 32 位 int 类型,我可以定义所有 32 位(包括符号指示位"),否则它们似乎不会作为二进制补码进行线程化.

Now half of the problem i have is that i use just 7 bits to describe what they tell me is a 8 bit data type. Second half is that they dont seem to be threaded as twos complement unless using a 32bit int type where i can define all of the 32 bits ("sign indicator bit" included).

现在,当我搜索如何显示范围内的数字 -128 时,我被告知要这样做,而没有任何进一步的解释:

Now when i search on how to display the in-range number -128 i was told to do it this way without any further explanation:

byte b8 = 0b1111_1111_1111_1111_1111_1111_1000_0000;

我可以清楚地看到最后 8 位 (1000 0000) 确实表示使用 8 位的二进制 -128,但我仍然从未感到困惑并尝试问我的问题:

I can clearly see that the last 8 Bit (1000 0000) do represent -128 in twos compelment using 8 Bit, still i never was confused more and try to ask my questions:

  • 上面的 32 位长 nr 不是 32 位 (java-) int 值吗?
  • 为什么我可以将 32 位值分配给 8 位 (java-) 字节类型?

或者一般来说:为什么我必须这样分配它?

任何有关此的链接/信息都会很棒!感谢您抽出宝贵时间阅读本文以及提前了解更多信息.

Any links/ informations about this would be great! Thank you for the time you took to read this as well as any further information in advance.

问候一月

推荐答案

根据Java规范,

http://docs.oracle.com/javase/specs/jls/se7/html/jls-3.html#jls-3.10.1

所有你的声明(b、b1、...和b8)使用int文字,即使它们适合一个字节.Java中没有字节字面量,只能用int来初始化一个字节.

all your declarations (b, b1,..., and b8) use int literals, even when they would fit in a byte. There's no byte literal in Java, you can only use an int to initialize a byte.

我做了一些测试,byte neg128 = -0b1000_0000; 工作正常.0b1000_0000 是 128,所以你只需要在它前面放一个 - 符号.请注意,1 根本不是符号位(不要考虑 8 位字节,考虑转换为字节的 32 位整数).因此,如果要指定符号位,则需要写入所有 32 位,如您所演示的.

I did some tests and byte neg128 = -0b1000_0000; works fine. 0b1000_0000 is 128, so you just need to put a - sign before it. Notice that that 1 is not a sign bit at all (don't think about 8-bit bytes, think about 32-bit ints converted to bytes). So if you want to specify the sign bit you need to write all 32 bits, as you have demonstrated.

所以 byte b8 = 0b1000_0000; 是一个错误,就像 byte b8 = 128; 是一个错误(+128 不适合一个字节).你也可以强制转换:

So byte b8 = 0b1000_0000; is an error just like byte b8 = 128; is an error (+128 does not fit in a byte). You can also force the conversion with a cast:

byte b = (byte) 0b1000_0000;或者byte b = (byte) 128;

强制转换告诉编译器您知道 128 不适合一个字节,并且位模式将被重新解释为 -128.

The cast tells the compiler that you know 128 does not fit in a byte and the bit-pattern will be reinterpreted as -128.

相关文章