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. HandlerMethodArgumentResolver完美解决 springmvc注入参数多传报错

    作为一个后端开发,能友好兼容前端参数传入错误等问题,在前端发布不小心多传一个参数导致系统错误的问题,一个广告系统是零容忍的,所以为了不犯错误,后端接收参数必须摒弃spring 的自动注入@Reques ...

  2. 查看tensorflow Pb模型所有层的名字

    代码如下: import tensorflow as tf def get_all_layernames(): """get all layers name"& ...

  3. Redis入门(三)——主从服务器配置

    当数据量变得庞大的时候,读写分离还是很有必要的.同时避免一个redis服务宕机,导致应用宕机的情况,我们启用sentinel(哨兵)服务,实现主从切换的功能.redis提供了一个master,多个sl ...

  4. 基于IAP的STM32程序更新技术

    引言 嵌入式系统的开发最终需要将编译好的代码下载到具体的微控制器芯片上,而不同厂家的微控制器芯片有不同的下载方式.随着技术的发展和应用需求的更新,用户程序加载趋向于在线编程的方式,越来越多的芯片公司提 ...

  5. nginx 之 root和alias

    转载:  https://www.jianshu.com/p/4be0d5882ec5 https://blog.csdn.net/Erica_1230/article/details/7855311 ...

  6. Java项目出现的问题--实际项目01

    1   从Excel中导入指纹图谱数据异常 在导入指纹图谱时对Excel的规范是有限制的,有时候报空指针异常是因为虽然有些地方看起来没有内容但是可能有空格键:解决方法是新建一个Excel表,把要用到的 ...

  7. 【Python之路】特别篇--微信Web网页版通信的全过程分析

    文章所使用Python版本为py3.5 1.微信服务器返回一个会话ID 微信Web版本不使用用户名和密码直接登录,而是采用二维码登录,所以服务器需要首先分配一个唯一的会话ID,用来标识当前的一次登录. ...

  8. react须知

    1. JSX是什么? 1)JSX是一种facebook发明的语法.就是将HTML和JS 可以同时书写.其实是一种js的语法糖. 但是浏览器不能识别,需要通过babel-loader来转译. @babe ...

  9. HDU 2176 取(m堆)石子游戏 —— (Nim博弈)

    如果yes的话要输出所有情况,一开始觉得挺难,想了一下也没什么. 每堆的个数^一下,答案不是0就是先取者必胜,那么对必胜态显然至少存在一种可能性使得当前局势变成必败的.只要任意选取一堆,把这堆的数目变 ...

  10. 使用 python 编写一个授权登录验证的模块

    使用 python 编写一个授权登录验证的模块 我们编写的思路: 1.登录的逻辑:如果用户名和密码正确,就返回 token . 2.生成 token 的逻辑,根据用户名,随机数,当前时间 + 2 小时 ...