按位或运算符对 2 的补码字节有什么特殊作用吗?

Does the bitwise OR operator do anything special to a 2's complement byte?

所以我需要深入研究 I2C 传感器读数的数字表示形式——对于 I2C 加速度计的读数,有一个步骤我无法绕过(粗体部分):

In order to read them all, we start with the first register, and the using the requestionFrom() function we ask to read the 6 registers. Then using the read() function, we read the data from each register, and because the outputs are twos complements we combine them appropriately to get the correct values.

// === Read acceleromter data === //
Wire.beginTransmission(ADXL345);
Wire.write(0x32); // Start with register 0x32 (ACCEL_XOUT_H)
Wire.endTransmission(false);
Wire.requestFrom(ADXL345, 6, true); // Read 6 registers total, each axis value is stored in 2 registers
X_out = ( Wire.read()| Wire.read() << 8); // X-axis value <--------------
X_out = X_out/256; //For a range of +-2g, we need to divide the raw values by 256, according to the datasheet
Y_out = ( Wire.read()| Wire.read() << 8); // Y-axis value
Y_out = Y_out/256;
Z_out = ( Wire.read()| Wire.read() << 8); // Z-axis value
Z_out = Z_out/256;

https://howtomechatronics.com/tutorials/arduino/how-to-track-orientation-with-arduino-and-adxl345-accelerometer/

在这里(在 Arduino 的 C++ 风格中)我们将 X_out 定义为浮点数,我们正在读取 X 轴的两个字节,其中 Wire.read()Wire.read() 位移向左 8 倍(<-------------- 行)。但是按位或操作如何(或者为什么)合并两个字节的信息?我无法通过谷歌搜索找到任何明确的答案。这是我不知道的标准吗?

按位或只是意味着输出输入的每一位的inclusive OR。与2的补码无关

所以如果我们有两个字节 X 和 Y,位模式分别为 abcdefghABCDEFGHX | Y << 8 的结果是这样的

Y             00000000ABCDEFGH
Y << 8        ABCDEFGH00000000
X             00000000abcdefgh
──────────────────────────────
X | Y << 8    ABCDEFGHabcdefgh

之所以有效,是因为任何与 0 的 OR 都等于其自身。只要看看 truth table,你就会 see.So A OR 0 = AB OR 0 = B0 OR a = a 等等

在这种情况下,+ 和 XOR 也可以工作,因为 0 XOR x = x0 + x = x。所以X + (Y << 8) == X ^ (Y << 8) == X | (Y << 8)