& 0xFF 取低8位

@Test
void byteTest() { byte hex1 = (byte) 127;
byte hex2 = (byte) 383;
byte hex3 = (byte) 0x7F; RandomAccessFile writeFile = null;
try {
//写入头文件
writeFile = new RandomAccessFile("D:\\temp\\hex.txt", "rw");
byte[] bytes = new byte[3];
bytes[0] = hex1;
bytes[1] = hex2;
bytes[2] = hex3;
writeFile.write(bytes, 0, bytes.length);
} catch (Exception ex) {
logger.error(ex.getMessage(), ex);
} finally {
try {
if (writeFile != null) {
writeFile.close();
}
} catch (Exception ex) { }
}
logger.info("成功");
}
@Test
void lowerOrderTest() {
// 65304 = 1111 1111 0001 1000 int h1 = ((65304)); //直接返回原数据 => 65304
int h2 = ((65304 >> 4)); //右移4位,高位补0 => 0000 1111 1111 0001 => 1111 1111 0001 => 4081 /**
* 取低8位(FF)
* 0xFFF = 11111111
* & 相同数为1 不同数为0
* 1111 1111 0001 1000
* 1111 1111
* 0000 0000 0001 1000
* 11000 => 24
*/
int h3 = ((65304) & 0xFF); /**
* >> 4 先右移4位, 高位补0
* 1111 1111 0001 1000
* 0000 1111 1111 0001
* & 0xFF 取低8位
* 0000 1111 1111 0001
* 1111 1111
* 0000 0000 1111 0001
* 11110001 => 241
*/
int h4 = ((65304 >> 4) & 0xFF); logger.info("\n{}\n{}\n{}\n{}",h1,h2,h3,h4);
}
 short、int 转byte数组
/**
* 32536 => 0111 1111 0001 1000 最左边为符号位
* 默认从低位到高位的顺序取值
* b[0] = 111 1111 0001 1000 & 0xFF = 0001 1000 = 24
* b[1] = 111 1111 0001 1000 >> 8 & 0xFF = 0111 1111 = 127
*/
public static byte[] shortToBytes(short shortValue, ByteOrder byteOrder) {
byte[] b = new byte[Short.BYTES];
if (ByteOrder.LITTLE_ENDIAN == byteOrder) {
b[0] = (byte) (shortValue & 0xFF);
b[1] = (byte) ((shortValue >> Byte.SIZE) & 0xff);
} else {
b[1] = (byte) (shortValue & 0xFF);
b[0] = (byte) ((shortValue >> Byte.SIZE) & 0xff);
}
return b;
} /**
* int 转 byte 同理
*/
public static byte[] intToBytes(int intValue, ByteOrder byteOrder) { if (ByteOrder.LITTLE_ENDIAN == byteOrder) {
return new byte[]{
(byte) (intValue & 0xFF),
(byte) ((intValue >> 8) & 0xFF),
(byte) ((intValue >> 16) & 0xFF),
(byte) ((intValue >> 24) & 0xFF)
}; } else {
return new byte[]{
(byte) ((intValue >> 24) & 0xFF),
(byte) ((intValue >> 16) & 0xFF),
(byte) ((intValue >> 8) & 0xFF),
(byte) (intValue & 0xFF)
};
}
}

& 0xFF 作用 取低8位的更多相关文章

  1. byte取高4位,低4位,byte转int

    byte abyte =-1; System.out.println(abyte); System.out.println(Integer.toBinaryString(abyte)); //取高四位 ...

  2. "用wow64exts调试64位任务管理器抓取的32位程序的dump"

    博客搬到了fresky.github.io - Dawei XU,请各位看官挪步.最新的一篇是:"用wow64exts调试64位任务管理器抓取的32位程序的dump".

  3. i/10和i取最后两位的精妙算法(前方高能)

    i/10; q2 = (i2 * 52429) >>> (16+3); 52429/524288 = 0.10000038146972656, 524288 = 1 << ...

  4. ARM微处理器中支持字节、半字、字三种数据类型,地址的低两位为0是啥意思?

    问题: ARM微处理器中支持字节.半字.字三种数据类型,其中,字需要4字节对齐(地址的低两位为0).半字需要2字节对齐(地址的最低位为0).我想问的是括号中的内容是什么意思呢?请牛人帮忙解释一下!谢谢 ...

  5. Python练习题 041:Project Euler 013:求和、取前10位数值

    本题来自 Project Euler 第13题:https://projecteuler.net/problem=13 # Project Euler: Problem 13: Large sum # ...

  6. java 实现小数取最后一位、四舍五入

    //获得最后一位 double a = 3.24; String b = String.valueOf(a); char c[] = b.toCharArray(); System.out.print ...

  7. SSH框架用法,及作用(在一位菜鸟使用半年之后归纳的总结)

    SSH框架从接触以来改变了我对代码的编写方式,从最初开始学习到勉强掌握可以说得到不少心得,以下内容出自java初学者对SSH的理解,如有不对的地方还请谅解,希望可以提出来与我交流,谢谢! SSH顾名思 ...

  8. excel 取前几位文字

    1L.2L的分别用mid函数和left函数都没有问题. 问题是,如果用left函数,必须先确认,字符串中汉字必须排在左边第一个,接下来几个也必须是汉字:mid函数则是根据从左边第某个字符开始,一共取几 ...

  9. PHP随手记2--获取随机n位不重复字符

    定义一个函数返回26英文字母中n位不重复随机字符 基本思路是利用内置函数生成随机数,取出该位置字母之后将其删除,再进行下一次随机,最后实现字符串拼接就ok! 代码很简单,通俗易懂,直接上代码吧: 1 ...

  10. 给出一个十六进制的数0xFF 0x80 (只有2“位”) 将其转换成有符号的一字节的十进制整数

    #include <stdio.h>#include<iostream>#include <stdlib.h>#include<string.h>usi ...

随机推荐

  1. jpa用findAll((Specification<GoodsSpu>) (root, criteriaQuery, criteriaBuilder) -> {})排序

    //需要用到的包import org.springframework.data.domain.Page;import org.springframework.data.domain.PageReque ...

  2. 题解 SP15454

    前言 数学符号约定 \(\operatorname{lowbit}(x)\):表示 \(x\) 的二进制最低位. \([a,b]\):表示区间 \(a\sim b\),其中包含 \(a,\,b\) 端 ...

  3. 03Java学习_注释和代码规范

    注释和代码规范 目录 注释和代码规范 注释 注释介绍 单行注释 多行注释 文档注释 代码规范 注释 注释介绍 用于注解说明解释程序的文字就是注释,注释提高了代码的阅读性(可读性):注释 是一个程序员必 ...

  4. Excel 使用 VLOOKUP 函数匹配特定列

    前言 工作有一项内容,是根据新的表格的某一列的内容一对一匹配,生成一列新的表格.这就用到了 Excel 的 VLOOKUP 函数. 函数使用 函数体: =VLOOKUP(lookup_value,ta ...

  5. 一款.NET开源的小巧、智能、免费的Windows内存清理工具 - WinMemoryCleaner

    前言 我们在使用Windows系统的时候经常会遇到一些程序不会释放已分配的内存,从而导致电脑变得缓慢.今天给大家推荐一款.NET开源的小巧.智能.免费的Windows内存清理工具:WinMemoryC ...

  6. 🔥🔥Java开发者的Python快速实战指南:探索向量数据库之文本搜索

    前言 如果说Python是跟随我的步伐学习的话,我觉得我在日常开发方面已经没有太大的问题了.然而,由于我没有Python开发经验,我思考着应该写些什么内容.我回想起学习Java时的学习路线,直接操作数 ...

  7. SQLBI_精通DAX课程笔记_03_计算列

    计算列是由DAX在表中生成的列,逐行计算并储存在模式之中. 以下链接是采悟老师关于度量值和计算列的区别的文章,可以同步查看. https://zhuanlan.zhihu.com/p/75462046 ...

  8. 前端优化之路:git commit 校验拦截

    [前言] 前面在git分支规范那篇文章里,介绍了commit提交规范,如下图 但是想要做到高效落地执行,就需要做些别的功课,先展示下成果图 没错,对不符合规范的commit进行了拦截,符合才可以成功提 ...

  9. VCS用法

    1.时钟频率点击,鼠标左键点击波形上升沿,中间滚轮点击,然后选择hz,就显示当前信号时钟频率. 2.窗口乱掉,找不到文件列表,右下角点击弹出选择instance. 3.bus地址查找,选择信号,然后蓝 ...

  10. HDU 4787 GRE Revenge

    Now Coach Pang is preparing for the Graduate Record Examinations as George did in 2011. At each day, ...