先看一段代码:

    @Test
public void test(){
byte a = -5;
byte b = 12;
System.out.println(a);
System.out.println(Integer.toHexString(a));
System.out.println(Integer.toHexString(a & 0xff));
System.out.println(b);
System.out.println(Integer.toHexString(b));
System.out.println(Integer.toHexString(b & 0xff));
}

执行结果:

-5
fffffffb
fb
12
c
c

解释:

1.负数在计算机中以补码形式保存,所以-5的二进制表示为11111011(负数补码的计算方式:绝对值的反码+1)

2.byte转换为int时,左边的24位补符号位,对于-5,转换后的二进制表示为11111111111111111111111111111011,这与之前的11111011,十进制都表示为-5,所以输出为-5

3.0xff为一个int整数,255,二进制表示为00000000000000000000000011111111,-5 & 0xff,即将-5的高24位置0

4.Integer.toHexString函数,循环右移,每次取4位,转换为16进制字符串,所以11111111111111111111111111111011转成16进制字符串,前面出现多次的'1111'都转成了'f'

Integer.toHexString源码如下:

/**
* Convert the integer to an unsigned number.
*/
//shift=4
private static String toUnsignedString(int i, int shift) {
char[] buf = new char[32];
int charPos = 32;
int radix = 1 << shift;
int mask = radix - 1;
do {
buf[--charPos] = digits[i & mask];
i >>>= shift;
} while (i != 0); return new String(buf, charPos, (32 - charPos));
} /**
* All possible chars for representing a number as a String
*/
final static char[] digits = {
'0' , '1' , '2' , '3' , '4' , '5' ,
'6' , '7' , '8' , '9' , 'a' , 'b' ,
'c' , 'd' , 'e' , 'f' , 'g' , 'h' ,
'i' , 'j' , 'k' , 'l' , 'm' , 'n' ,
'o' , 'p' , 'q' , 'r' , 's' , 't' ,
'u' , 'v' , 'w' , 'x' , 'y' , 'z'
};

(byte & 0xff)操作的更多相关文章

  1. byte & 0xff char 转换

    https://blog.csdn.net/lixingtao0520/article/details/75450883 版权声明:本文为博主原创文章,转载请注明作者与出处,http://blog.c ...

  2. System.Buffer 以字节数组(Byte[])操作基元类型数据

    1. Buffer.ByteLength:计算基元类型数组累计有多少字节组成. 该方法结果等于"基元类型字节长度 * 数组长度" , , }; , , }; , , }; Cons ...

  3. 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

    'utf-8' codec can't decode byte 0xff in position 0: invalid start byte 觉得有用的话,欢迎一起讨论相互学习~Follow Me 今 ...

  4. python3.4 UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position

    python3.4 UnicodeDecodeError: 'gbk' codec can't decode byte 0xff in position 实用python的时候 打开一个csv的文件出 ...

  5. TensorFlow学习笔记(UTF-8 问题解决 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte)

    我使用VS2013  Python3.5  TensorFlow 1.3  的开发环境 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff ...

  6. tensorflow UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid start byte

    tensorflow读取图像出现错误:UnicodeDecodeError: 'utf-8' codec can't decode byte 0xff in position 0: invalid s ...

  7. TensorFlow学习('utf-8' codec can't decode byte 0xff in position 0: invalid start byte)

    使用语句: image_raw_data = tf.gfile.GFile("./picture.jpg", "r").read() 读取图像时报错如下: Un ...

  8. Java byte位移操作 注意事项

    Java对byte 的 + - * / >> >>> << & | ^ (加,减,乘,除,右移,左移,无符号右移,位与,位或,位异或)操作,均会是首先 ...

  9. Java byte 位移操作 注意事项

    转自:http://blog.163.com/pilgrim_yang/blog/static/55631481201111542151582/ Java对byte 的 + - * / >> ...

随机推荐

  1. windbg 分析cpu异常

    1.   !threadpool  查看当前CPU状况 线程数等等 2.   !runaway 查看那几个线程使用的高 建议多抓几个dump 然后确定到底是哪个线程 3.   ~线程IDs 跳转到那个 ...

  2. (buuctf) - pwn入门部分wp - rip -- pwn1_sctf_2016

    [buuctf]pwn入门 pwn学习之路引入 栈溢出引入 test_your_nc [题目链接] 注意到 Ubuntu 18, Linux系统 . nc 靶场 nc node3.buuoj.cn 2 ...

  3. 换系统之后为什么iMindMap会提示“许可证使用的次数过多”

    iMindMap是一款十分受欢迎的思维导图软件,随着12版本的上线,iMindMap新增了很多新用户,最近小编发现有不少新用户在群里反映:"为什么购买iMindMap时说可以支持换机,但是在 ...

  4. H5系列之contenteditable

    其实这个属性很简单,既然把它放到一个单独的文章来说,他肯定有一些注意点要讲 兼容性很好,兼容所有主流浏览器. 用法很简单,只需要给你需要的标签填上即可. <div contenteditable ...

  5. C中memcpy函数用法

    1.函数原型 void *memcpy(void *destin,void *source,unsigned n); 其中, destin代表用于存储复制内容的目标数组,类型强制转换为void*指针. ...

  6. C语言printf()函数的格式化字符串

    原文链接:https://www.runoob.com/cprogramming/c-function-printf.html#include<stdio.h> #include<s ...

  7. Java Stream 源码分析

    前言 Java 8 的 Stream 使得代码更加简洁易懂,本篇文章深入分析 Java Stream 的工作原理,并探讨 Steam 的性能问题. Java 8 集合中的 Stream 相当于高级版的 ...

  8. ccpc赛前记

    距离ccpc比赛还不到一个小时了,有些紧张又有些兴奋 作为留学选手参加国内的比赛感觉好像很奇怪?谁能想到一个疫情会让我拿ccpc结束自己的acm生涯(也许,谁知道呢) cf上蓝了 该准备gre了,目标 ...

  9. linux搭建harbor与使用

    条件:安装docker&docker-compose 如未安装,请看:linux离线安装docker + docker-compose harbor 1.下载 下载地址:https://git ...

  10. 倾斜摄影实景三维在智慧工厂 Web 3D GIS 数字孪生应用

      数字化推动钢铁工业转型升级 数字时代,随着数字地球,数字中国,数字工厂等数字化建设的不断深入,以地理信息系统(Geographic Information System, GIS)为基础,融合大数 ...