Java Unsigned Bytes
Having had to use unsigned bytes for the first time, I also had to learn how Java references these datatypes. I did some research, and found a number of good guides out there but was generally dissatisfied with the approach they took to learn the subject matter.
Unsigned Data Types
Java data types are all signed* and are all in Big Endian byte order. An unsigned byte is represented by the following 8 bits:
0 0 0 0 0 0 0 0
128 64 32 16 8 4 2 1
The range of values that can be represented is 0 through to 255. A number like 56 is represented as:
0 0 1 1 1 0 0 0
128 64 32 16 8 4 2 1
However a signed byte uses its Most Significant Bit (MSB) to control the the sign of the number. Java uses a technique known as Two's Complement to perform arithmetic on the numbers. The result of using the MSB as a sign bit is that is changes the range of numbers to -128 through to 127. We don't need to understand how Two's Complement to understand that it changes the byte as follows:
0 0 0 0 0 0 0 0
+/- 64 32 16 8 4 2 1
Note: Both unsigned and signed are just ways of representing a byte. A byte has meaning in either representation.
Conversion to unsigned byte
To convert a number into an unsigned byte, you have to use a larger data type to store the number. So take for example a value 183, must be represented by a data type larger than a byte. Any of the following, short, int, long or double will do. In this case we'll use an integer as all binary operators work with integers which makes them convenient to use.
int i = 183;
00000000 00000000 00000000 10110111 = signed int 183
Then we cast the integer to a byte. This effectively chops the bits at the 8th bit.
byte b = (byte)i;
writeAByteToDisk( b );
10110111 = unsigned byte 183
signed byte -73
This byte can then be written out to file, or network channel in the case that you are interacting with an application that expects an unsigned byte.
Conversion from unsigned byte
Converting from an unsigned byte into an integer we first cast the byte to an integer. This cast takes place with sign extension where by the Most Significant Bit represents the sign of the value and is duplicated up until it forms an integer.
byte b = readAByteFromDisk();
int i = (int)b; 11011011 = unsigned byte 219
^ signed byte -37
|_________ Most Significant Bit 11111111 11111111 11111111 11011011 = signed int -37
^
|____________________________________ extended to make integer
Then we apply a bit mask using the binary AND operator. This effectively masks out the sign extension that has taken place in the cast to an integer.
int i = 0x000000FF & i; 11111111 11111111 11111111 11011011 = signed int -37
00000000 00000000 00000000 11111111 = Mask of 0x000000FF
00000000 00000000 00000000 11011011 = Result of & operation
We use the Hex number 0x000000FF (255) as the mask becuase this will cover the first 8 bits of the integer and the area we are interested in. The entire function can be shorted to a more conscise statement. A byte value, when used with the & operator automatically casts the byte to an integer. The Hex value of 0x000000FF can be represented by 0xFF (the 0's are added automatically up to the size of the integer).
int i = 0xFF & readAByteFromDisk();
Related sites
Java Unsigned Bytes的更多相关文章
- PostgreSQL java读取bytes字段
写入bytea: File img = new File("/tmp/eclipse.png"); fin = new FileInputStream(img); con = Dr ...
- Java and unsigned int, unsigned short, unsigned byte, unsigned long, etc. (Or rather, the lack thereof)
http://darksleep.com/player/JavaAndUnsignedTypes.html —————————————————————————————————————————————— ...
- Java Bytecode Instructions List
monic Opcode(in hex) Other bytes Stack[before]→[after] Description aaload 32 arrayref, index → val ...
- JVM Specification 9th Edition (3) Chapter 2. The Structure of the Java Virtual Machine
Chapter 2. The Structure of the Java Virtual Machine 内容列表 2.1. The class File Format (class文件的格式) 2. ...
- java 字节码指令集
This is a list of the instructions that make up the Java bytecode, an abstract machine language that ...
- py, pyc, pyw, pyo, pyd Compiled Python File (.pyc) 和Java或.NET相比,Python的Virtual Machine距离真实机器的距离更远
https://my.oschina.net/renwofei423/blog/17404 1. PyCodeObject与Pyc文件 通常认为,Python是一种解释性的语言,但是这种说法 ...
- java String.getBytes()编码问题——String.getBytes(charset)
String的getBytes()方法是得到一个字串的字节数组,这是众所周知的.但特别要注意的是,本方法将返回该操作系统默认的编码格式的字节数组.如果你在使用这个方法时不考虑到这一点,你会发现在一个平 ...
- Java fundamentals of basic IO
IO is a problem difficult to handle in various of systems because it always becomes a bottleneck in ...
- 深入解析java String中getBytes()的编码问题
转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6900536.html Java服务器后台在和Android端App通信时,遇到了两端关于用MD5加密同一包含 ...
随机推荐
- Java重写(Override)与重载(Overload)
方法的重写规则 参数列表必须完全与被重写方法的相同: 返回类型与被重写方法的返回类型可以不相同,但是必须是父类返回值的派生类(java5 及更早版本返回类型要一样,java7 及更高版本可以不同): ...
- 说一下 HashSet 的实现原理?(未完成)
说一下 HashSet 的实现原理?(未完成)
- Loadrunner12-思考时间设置
1.什么是思考时间 简单来说就是可以在不同的操作之间做停顿,最大程度的模拟用户最真实的操作. 2.如何设置思考时间 函数:lr_think_time(4) 进入Runtime Settings页面,快 ...
- Java中的super的使用
- Spring实战(第4版)
第1部分 Spring的核心 Spring的两个核心:依赖注入(dependency injection,DI)和面向切面编程(aspec-oriented programming,AOP) POJO ...
- POI读取格式化后的单元格数据
public static String getFormattedValue(Cell cell) { FormulaEvaluator evaluator = cell.getSheet().get ...
- 【Python之路】特别篇--Django瀑布流实现
瀑布流 瀑布流,又称瀑布流式布局.是比较流行的一种网站页面布局,视觉表现为参差不齐的多栏布局,随着页面滚动条向下滚动,这种布局还会不断加载数据块并附加至当前尾部.最早采用此布局的网站是Pinteres ...
- 将.py脚本打包成.exe
https://www.cnblogs.com/wyl-0120/p/10823102.html 为了方便使用,通过pyinstaller对脚本进行打包成exe文件. pip3 install pyi ...
- java new一个对象的过程中发生了什么
java在new一个对象的时候,会先查看对象所属的类有没有被加载到内存,如果没有的话,就会先通过类的全限定名来加载.加载并初始化类完成后,再进行对象的创建工作. 我们先假设是第一次使用该类,这样的话n ...
- 在Idea中 的terminal 使用 git
参考该博客内容 http://blog.csdn.net/qq_28867949/article/details/73012300