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的更多相关文章

  1. PostgreSQL java读取bytes字段

    写入bytea: File img = new File("/tmp/eclipse.png"); fin = new FileInputStream(img); con = Dr ...

  2. Java and unsigned int, unsigned short, unsigned byte, unsigned long, etc. (Or rather, the lack thereof)

    http://darksleep.com/player/JavaAndUnsignedTypes.html —————————————————————————————————————————————— ...

  3. Java Bytecode Instructions List

    monic Opcode(in hex) Other bytes Stack[before]→[after] Description aaload 32   arrayref, index → val ...

  4. 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. ...

  5. java 字节码指令集

    This is a list of the instructions that make up the Java bytecode, an abstract machine language that ...

  6. 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是一种解释性的语言,但是这种说法 ...

  7. java String.getBytes()编码问题——String.getBytes(charset)

    String的getBytes()方法是得到一个字串的字节数组,这是众所周知的.但特别要注意的是,本方法将返回该操作系统默认的编码格式的字节数组.如果你在使用这个方法时不考虑到这一点,你会发现在一个平 ...

  8. Java fundamentals of basic IO

    IO is a problem difficult to handle in various of systems because it  always becomes a bottleneck in ...

  9. 深入解析java String中getBytes()的编码问题

    转载请注明出处:http://www.cnblogs.com/Joanna-Yan/p/6900536.html Java服务器后台在和Android端App通信时,遇到了两端关于用MD5加密同一包含 ...

随机推荐

  1. vue2.0+webpack+vuerouter+vuex+axios构建项目基础

    前言 本文讲解的是vue2.0+webpack+vuerouter+vuex+axios构建项目基础 步骤 1.全局安装webpack,命令 npm install webpack -g 注意,web ...

  2. mysql 数据库常见的一些基本操作 !详不详细你说了算!

    在日常应用中可能一时想不起来,所以有必要整理一份 指令相关的笔记,以是个人比较满意,也比较全面的一份笔记,希望能帮到你,适用初级小白,大神可略过! MYSQL常用命令: 数据备份与还原·注意:不要打分 ...

  3. dao层取值用List<map<String,Object>>接收有序map

    发现一个好玩的Map, 当需要Map有序时用java.util.LinkedHashMap接收,是有序map resultType="java.util.LinkedHashMap" ...

  4. ShedLock日常使用

    首发于个人博客:ShedLock日常使用 场景模拟 定时器Scheduler在平时使用比较频繁,比如定时数据整理,定时向客户发送问候信息等...,定时任务的配置比较简单,比如在springboot中, ...

  5. docker 在push镜像到本地registry出现的500 Internal Server Error

    ]# docker push 192.168.163.131:5000/test The push refers to a repository [192.168.163.131:5000/test] ...

  6. UOJ117 欧拉回路[欧拉回路]

    找欧拉回路的模板题. 知识点详见图连通性学习笔记. 注意一些写法上的问题. line37&line61:因为引用,所以j和head值是同步更新的,类似于网络流的当前弧优化,除了优化枚举外,这样 ...

  7. BZOJ2208 [Jsoi2010]连通数[缩点/Floyd传递闭包+bitset优化]

    显然并不能直接dfs,因为$m$会非常大,复杂度就是$O(mn)$: 这题有三种做法,都用到了bitset的优化.第二种算是一个意外的收获,之前没想到竟然还有这种神仙操作.. 方法一:缩点+DAG上b ...

  8. Hbuilder + MUI 的简单案例

    话不多说 直接上代码 项目结构: index.html 的代码 <!DOCTYPE html><html>    <head>        <meta ch ...

  9. Neo4j入门博客分享

    Neo4j学习参考博客:https://www.cnblogs.com/ljhdo/p/5516793.html Neo4j Cypher查询语言详解 http://www.ttlsa.com/nos ...

  10. Java8-Stream-No.13

    import java.security.SecureRandom; import java.util.Arrays; import java.util.stream.IntStream; publi ...