Java中的LSB / MSB处理

编程入门 行业动态 更新时间:2024-10-10 09:16:50
本文介绍了Java中的LSB / MSB处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

如果我必须处理以0x118为单位存储的值,我该如何拆分LSB和MSB?

If I have to handle values to be stored in bytes like 0x118, how do I split the LSB and MSB?

我正在尝试以下方式...我不认为这是正确的方式:

I was trying the following way... I don't think that it's the right way:

value = 0x118;

以字节存储...

result[5] = (byte) value; result[6] = (byte)(value << 8); ...

正确的方法是什么?

推荐答案

这样做:

result[5] = (byte) (value & 0xFF); // Least significant "byte" result[6] = (byte) ((value & 0xFF00) >> 8); // Most significant "byte"

我通常使用位掩码 - 也许他们不需要。第一行选择低8位,第二行选择高8位,并将位8位位置向右移位。这相当于除以2 8 。

I usually use bit masks - maybe they're not needed. The first line selects the lower eight bits, the second line selects the upper eight bits and shifts the bits eight bit positions to the right. This is equal to a division by 28.

这是背后的诡计 :

(I) LSB 01010101 10101010 // Input & 00000000 11111111 // First mask, 0x00FF ----------------- 00000000 10101010 // Result - now cast to byte (II) MSB 01010101 10101010 // Input & 11111111 00000000 // Second mask, 0xFF00 ----------------- 01010101 00000000 // Result - >>>>>>>> // "Shift" operation, eight positions to the right ----------------- 00000000 01010101 // Result - now cast to byte

总结一下,做以下计算:

To sum it up, do the following calculation:

byte msb = result[6]; byte lsb = result[5]; int result = (msb << 8) + lsb; // Shift the MSB bits eight positions to the left.

更多推荐

Java中的LSB / MSB处理

本文发布于:2023-11-28 21:32:05,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1644016.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Java   LSB   MSB

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!