久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Java 二進制文字 - 字節值 -128

Java binary literals - Value -128 for byte(Java 二進制文字 - 字節值 -128)
本文介紹了Java 二進制文字 - 字節值 -128的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

由于 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模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

How to wrap text around components in a JTextPane?(如何在 JTextPane 中的組件周圍環繞文本?)
MyBatis, how to get the auto generated key of an insert? [MySql](MyBatis,如何獲取插入的自動生成密鑰?[MySql])
Inserting to Oracle Nested Table in Java(在 Java 中插入 Oracle 嵌套表)
Java: How to insert CLOB into oracle database(Java:如何將 CLOB 插入 oracle 數據庫)
Why does Spring-data-jdbc not save my Car object?(為什么 Spring-data-jdbc 不保存我的 Car 對象?)
Use threading to process file chunk by chunk(使用線程逐塊處理文件)
主站蜘蛛池模板: 性色网站 | 国产精品一区二区三区在线 | 日韩高清一区 | av中文字幕在线播放 | 国产精品久久久久久吹潮 | 国产区在线观看 | 亚洲伊人精品酒店 | 午夜免费看视频 | 国产精品视屏 | 黄色av网站在线免费观看 | 国产精品一区二区三区在线 | 天天视频一区二区三区 | 亚洲性视频网站 | 欧美久久国产精品 | 男人天堂手机在线视频 | 精品无码三级在线观看视频 | 久久亚洲一区 | 成人av在线播放 | 亚洲+变态+欧美+另类+精品 | 亚洲天堂中文字幕 | 国内精品一区二区三区 | 国产福利在线 | 国产视频不卡一区 | 欧美激情欧美激情在线五月 | 国产精品久久久久久一级毛片 | 日韩在线免费视频 | 在线观看毛片网站 | 91视频入口| 亚洲一区二区三区在线免费观看 | 噜啊噜在线 | 欧美在线观看一区 | 日本精品久久久久久久 | 欧美日韩在线播放 | 嫩草研究影院 | 91久久看片 | 亚洲一区二区三区在线播放 | 国产一区影院 | 中文字幕色站 | 国产偷录叫床高潮录音 | 国产精品成人久久久久 | 久久精品a级毛片 |