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

理解 Java 字節

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

問題描述

所以昨天在工作中,我不得不編寫一個應用程序來計算 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模板網!

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

相關文檔推薦

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(使用線程逐塊處理文件)
主站蜘蛛池模板: 日本黄视频在线观看 | 欧美一级做a爰片免费视频 国产美女特级嫩嫩嫩bbb片 | 国产午夜三级一区二区三 | 色一情一乱一伦一区二区三区 | 亚洲色欲色欲www | 亚洲成人av | 午夜电影一区 | 精品国产免费人成在线观看 | 国产一级特黄视频 | 久久69精品久久久久久久电影好 | 国产精品成人久久久久a级 久久蜜桃av一区二区天堂 | 午夜精品一区二区三区在线观看 | 欧美日韩网站 | 日韩在线视频一区二区三区 | 国产精品视频综合 | 荷兰欧美一级毛片 | 日韩高清成人 | 中文字幕视频三区 | 自拍偷拍亚洲视频 | 欧美一a一片一级一片 | 欧美成人一区二区 | 免费污视频 | www.婷婷亚洲基地 | 久久亚洲一区二区三区四区 | 日日夜夜精品视频 | 中文字幕av色 | www.欧美.com| 日韩精品一区二区三区在线播放 | 日本成人中文字幕 | 国产精品www | 久久毛片 | 欧美成年人 | 久久精品视频网站 | 国产亚洲人成a在线v网站 | 中文字幕一区二区三区精彩视频 | 99re视频在线观看 | 午夜欧美一区二区三区在线播放 | 男女视频在线观看 | 国产精品一区久久久久 | 亚洲精品片 | 亚洲精品视频在线 |