問題描述
由于 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.
這篇關于Java 二進制文字 - 字節值 -128的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!