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加密同一包含 ...
随机推荐
- golang多维数组的切片
通过for循环来取多维数组的切片 package main import ( "fmt" ) func main() { a := [...]string{"USA&qu ...
- CentOS7 内核优化 修改参数
一:内核简介 内核是操作系统最基本的部分.它是为众多应用程序提供对计算机硬件的安全访问的一部分软件,这种访问是有限的,并且内核决定一个程序在什么时候对某部分硬件操作多长时间. 内核的分类可分为单内核和 ...
- 利用Struts上传文件
在利用struts2完成上传文件到服务器时,遇到获取不到文件名 原因是在Action中的属性名没有和jsp中的属性名匹配 <%@ page language="java" i ...
- linear 工作流
最近把 最后一张画ui的rt 从float换成srgb 并没有引起我预计会有的 alpha混合结果发生变化的事情 我想是因为 1.artists在ps里的工作流是线性空间 2.onchip memor ...
- hdu 3555 Bomb(数位dp入门)
Bomb Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 131072/65536 K (Java/Others) Total Subm ...
- Python数据抓取(2) —简单网络爬虫的撰写
(一)使用Requests存储网页 Requests 是什么?网络资源(URLs)抓取套件 优点? 改善urllib2的缺点,让使用者以最简单的方式获取网络资源 可以使用REST操作(POST,PUT ...
- 看透“0”、“1”逻辑,轻松解决Python中文乱码
Python中关于"中文乱码"的问题,现规整如下,并统一回答 同学问: jacky:我在爬取XX网站信息的时候,中文怎么总是显示的乱码? jacky:UTF-8与GBK到底是个啥? ...
- 分布式缓存Redis之Pipeline(管道)
Redis的pipeline(管道)功能在命令行中没有,但redis是支持pipeline的,而且在各个语言版的client中都有相应的实现. 由于网络开销延迟,就算redis server端有很强的 ...
- CentOS7遇到问题总结
问题1.保护多库版本:libstdc++-4.8.5-28.el7_5.1.i686 != libstdc++-4.8.5-28.el7.x86_64 错误:保护多库版本:libgcc-4.8.5-2 ...
- Linux .bashrc
Set environmental variable // 用:隔开 export PATH=$PATH:<PATH >:<PATH >:<PATH >:----- ...