简体   繁体   English

Java中的24到32位转换

[英]24- to 32-bit conversion in Java

I am interested in a new IoT project called OpenBCI , which is basically an open source EEG for reading and processing brain waves and other biodata. 我对一个名为OpenBCI的新物联网项目感兴趣 ,该项目基本上是一个用于读取和处理脑电波和其他生物数据的开源脑电图。 In their docs they claim that the data transmitted over-the-air (via RFDuino) sends 24-bit data. 在他们的文档中,他们声称通过空中传输的数据(通过RFDuino)发送24位数据。 To convert the 24-bit values into 32-bit signed integers, they suggest the following Java-friendly Processing code: 要将24位值转换为32位有符号整数,他们建议使用以下Java友好的处理代码:

int interpret24bitAsInt32(byte[] byteArray) {
    int newInt = (
        ((0xFF & byteArray[0]) << 16) |
        ((0xFF & byteArray[1]) << 8) |
        (0xFF & byteArray[2])
    );

    if ((newInt & 0x00800000) > 0) {
        newInt |= 0xFF000000;
    } else {
        newInt &= 0x00FFFFFF;
    }

    return newInt;
}

I guess I'm trying to understand exactly what is going on here. 我想我正在努力了解到底发生了什么。 Let's take the first blurb of code: 让我们来看看第一段代码:

int newInt = (
    ((0xFF & byteArray[0]) << 16) |
    ((0xFF & byteArray[1]) << 8) |
    (0xFF & byteArray[2])
);
  • Why is it safe to assume there are 3 bytes in the input? 为什么假设输入中有3个字节是安全的?
  • What is the significance of the 0xFF value? 0xFF值有什么意义?
  • What is the purpose of the left-bitshifting ( << )? 左位移( << )的目的是什么?

The second segment is also a bit of a mystery: 第二部分也有点神秘:

if ((newInt & 0x00800000) > 0) {
    newInt |= 0xFF000000;
} else {
    newInt &= 0x00FFFFFF;
}
  • Why newInt & 0x00800000 ? 为什么newInt & 0x00800000 What's the significance of 0x00800000 ? 0x00800000什么意义?
  • Why the if-else based on postive vs. non-negative result of the above operation? 为什么if-else基于上述操作的积极与非阴性结果?
  • What is the significance of 0xFF000000 and 0x00FFFFFF ? 0xFF0000000x00FFFFFF什么意义?

I guess there's just a lot of handwaving and magic going on with this function that I'd like to understand better! 我想这个功能只有很多手工和魔法,我想更好地理解!

Why is it safe to assume there are 3 bytes in the input? 为什么假设输入中有3个字节是安全的?

24 bits / 8 bits = 3 bytes 24位/ 8位= 3个字节

What is the significance of the 0xFF value? 0xFF值有什么意义?

It's a bit mask. 这有点面具。 0b11111111 in binary. 0b11111111二进制。 In this situation it's used as a quick way to convert the byte value into an int. 在这种情况下,它被用作将字节值转换为int的快速方法。

What is the purpose of the left-bitshifting (<<)? 左位移(<<)的目的是什么?

Once the int value has been retrieved in the previous instruction it's shifted 16 bits to the left to take the position of the third byte of the int (counting from the right). 一旦在前一条指令中检索到int值,它就会向左移16位以获取int的第三个字节的位置(从右边开始计数)。 This is due to the byte array storing the bytes in big-endian format, where the MSB comes first. 这是由于字节数组以big-endian格式存储字节,其中MSB首先出现。 The next byte is only shifted 8 bits to take up the position of the second byte, and the last one does not need to be shifted at all since it's already in the the first byte spot. 下一个字节仅移位8位以占用第二个字节的位置,最后一个字节根本不需要移位,因为它已经在第一个字节点中。

Why newInt & 0x00800000? 为什么newInt和0x00800000? What's the significance of 0x00800000? 0x00800000有什么意义?

Well 0x80 is another bit mask to get the MSB of a certain byte. 0x80是获得某个字节的MSB的另一个位掩码。 0x00800000 corresponds to the MSB of the third byte (ie the byte in byteArray[0] that was shifted 16 bits left in the previous process). 0x00800000对应于第三个字节的MSB(即byteArray[0]中的字节,在前一个过程中向左移位了16位)。 This also corresponds to the MSB of the whole 24-bit value. 这也对应于整个24位值的MSB。

Why the if-else based on postive vs. non-negative result of the above operation? 为什么if-else基于上述操作的积极与非阴性结果?

Determining whether the MSB of the 24-bit value is 1 or 0 will tell us if it's negative or positive number, respectively, in two's complement notation. 确定24位值的MSB是1还是0将分别以二进制补码表示法告诉我们它是负数还是正数。

What is the significance of 0xFF000000 and 0x00FFFFFF? 0xFF000000和0x00FFFFFF有什么意义?

If the number is negative in it's 24 bit representation, we also want it to be negative as a 32-bit value in two's complement, that's why we have to fill up the 4th and final byte with 0b11111111 (0xFF) using the OR operator. 如果数字为24位表示为负,我们也希望它为负二进制补码中的32位值,这就是为什么我们必须使用OR运算符用0b11111111 (0xFF)填充第4个和最后一个字节。
If it was already positive then the fourth byte can be 0x00, with the following 3 bytes being the same as the original 24 bit value - accomplished by masking with 0x00FFFFFF . 如果它已经为正,则第四个字节可以是0x00,后面的3个字节与原始的24位值相同 - 通过使用0x00FFFFFF屏蔽完成。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM