問題描述
所以昨天在工作中,我不得不編寫一個應用程序來計算 AFP 文件中的頁數.所以我翻閱了我的 MO:DCA 規范 PDF 并找到了結構化字段 BPG (Begin Page)
及其 3 字節標識符.該應用程序需要在 AIX 機器上運行,因此我決定用 Java 編寫它.
So at work yesterday, I had to write an application to count the pages in an AFP file. So I dusted off my MO:DCA spec PDF and found the structured field BPG (Begin Page)
and its 3-byte identifier. The app needs to run on an AIX box, so I decided to write it in Java.
為了獲得最大效率,我決定讀取每個結構化字段的前 6 個字節,然后跳過該字段中的剩余字節.這會讓我:
For maximum efficiency, I decided that I would read the first 6 bytes of each structured field and then skip the remaining bytes in the field. This would get me:
0: Start of field byte
1-2: 2-byte length of field
3-5: 3-byte sequence identifying the type of field
所以我檢查字段類型,如果它是 BPG
,我會增加一個頁面計數器,如果不是,我不會.然后我跳過字段中的剩余字節而不是通讀它們.在這里,在跳過(實際上是在字段長度中)是我發現 Java 使用有符號字節的地方.
So I check the field type and increment a page counter if it's BPG
, and I don't if it's not. Then I skip the remaining bytes in the field rather than read through them. And here, in the skipping (and really in the field length) is where I discovered that Java uses signed bytes.
我做了一些谷歌搜索,發現了很多有用的信息.當然,最有用的指令是按位執行 &
到 0xff
以獲得 unsigned int 值.這對于我獲得可用于計算要跳過的字節數的長度是必要的.
I did some googling and found quite a bit of useful information. Most useful, of course, was the instruction to do a bitwise &
to 0xff
to get the unsigned int value. This was necessary for me to get a length that could be used in the calculation for the number of bytes to skip.
我現在知道在 128 時,我們從 -128 開始倒數.我想知道的是這里的按位運算是如何工作的——更具體地說,我是如何得出負數的二進制表示的.
I now know that at 128, we start counting backwards from -128. What I want to know is how the bitwise operation works here--more specifically, how I arrive at the binary representation for a negative number.
如果我正確理解按位 &
,則您的結果等于一個僅設置兩個數字的公共位的數字.所以假設 byte b = -128
,我們會有:
If I understand the bitwise &
properly, your result is equal to a number where only the common bits of your two numbers are set. So assuming byte b = -128
, we would have:
b & 0xff // 128
1000 0000-128
1111 1111 255
---------
1000 0000 128
那么我如何以 -128 獲得 1000 0000?我如何獲得像 -72 或 -64 這樣不太明顯的東西的二進制表示?
So how would I arrive at 1000 0000 for -128? How would I get the binary representation of something less obvious like -72 or -64?
推薦答案
為了獲得負數的二進制表示,你需要計算二進制補碼:
In order to obtain the binary representation of a negative number you calculate two's complement:
- 獲取正數的二進制表示
- 反轉所有位
- 添加一個
我們以-72為例:
0100 1000 72
1011 0111 All bits inverted
1011 1000 Add one
所以-72的二進制(8位)表示是10111000
.
So the binary (8-bit) representation of -72 is 10111000
.
實際發生在您身上的是:您的文件有一個值為 10111000
的字節.當解釋為無符號字節(這可能是您想要的)時,這是 88.
What is actually happening to you is the following: You file has a byte with value 10111000
. When interpreted as an unsigned byte (which is probably what you want), this is 88.
在Java中,當這個字節被用作一個int時(例如因為read()
返回一個int,或者因為隱式提升),它會被解釋為一個有符號字節,并且符號- 擴展至 11111111 11111111 11111111 10111000
.這是一個值為 -72 的整數.
In Java, when this byte is used as an int (for example because read()
returns an int, or because of implicit promotion), it will be interpreted as a signed byte, and sign-extended to 11111111 11111111 11111111 10111000
. This is an integer with value -72.
通過與 0xff
進行與運算,您只保留最低 8 位,因此您的整數現在是 00000000 00000000 00000000 10111000
,即 88.
By ANDing with 0xff
you retain only the lowest 8 bits, so your integer is now 00000000 00000000 00000000 10111000
, which is 88.
這篇關于理解 Java 字節的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!