& 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. Java Springbool敏感词过工具类滤

    Java Springbool敏感词过工具类滤 1. 功能描述利用前缀树这种数据结构,设计并开发出敏感词过滤工具. 2. 构建敏感词表resource/sensitive-words.txt 3. 敏 ...

  2. 2021CSP 游记

    总结 试机日: 我天,这学校什么垃圾电脑-- 比赛日: 1. 普及考试 总体上来说题目算简单 (只是我脑残),t1简单 \(O(1)\),学了数论就行,而 t2 看懂后按题意打一遍,再优化一下: 数组 ...

  3. Vite4+Typescript+Vue3+Pinia 从零搭建(3) - vite配置

    项目代码同步至码云 weiz-vue3-template 关于vite的详细配置可查看 vite官方文档,本文简单介绍vite的常用配置. 初始内容 项目初建后,vite.config.ts 的默认内 ...

  4. [动态树] Link-Cut Tree

    Link-Cut Tree 0x00 绪言 学长们讲 LCT 的时候,我在另一个机房摸鱼,所以没有听到,就回家看 yxc 的补了补. 0x01 什么是动态树 动态树问题, 即要求我们维护一个由若干棵子 ...

  5. C#不用正则校验密码(附赠正则表达式)

    群聊 群友A问:有没有方法可以判断字符串里面带不带标点符号啊? 群友B问:需求是什么? 群友A说:想要密码校验,网上大部分都是正则. 群友A说:密码规则是包含大小写和数字.特殊符号,还有Length& ...

  6. python类的赋值操作和浅拷贝

    变量的赋值 只是形成了两个变量,但是还是指向同一个对象,我们来看看代码 class SWS: pass class SSN: pass class C: def __init__(self, sws, ...

  7. Echarts 柱形图最全详解

    Echarts 是一款基于 JavaScript 的开源可视化图表库,被广泛应用于数据可视化领域.它提供了丰富的图表类型和交互功能,其中柱形图是最常用和重要的一种图表类型之一.下面是对 Echarts ...

  8. 连续无创式血压估算cNIBP

    参考来源:ADI官网技术文章.知乎(hxl695822705.埃微许伯.Paddington.Lonerpaul.政聪.KingPo-张超.啪啦钉子.深圳加1健康科技) 参考文献:基于单路PPG信号的 ...

  9. vue-test --------ref

    <template> <div ref="contain">{{content}}</div> <button @click=" ...

  10. Cloudeye对接Prometheus实现华为云全方位监控

    本文分享自华为云社区<Cloudeye对接Prometheus实现华为云全方位监控>,作者:可以交个朋友 . 一. 背景 云眼系统Cloudeye服务为我们提供了针对弹性云服务器.宽带等资 ...