C++对代码的解释

C++ Explanation of the code

本文关键字:解释 代码 C++      更新时间:2023-10-16

我有一个串行读取的数组,名为sensor_buffer。它包含21个字节。

gyro_out_X=((sensor_buffer[1]<<8)+sensor_buffer[2]);
gyro_out_Y=((sensor_buffer[3]<<8)+sensor_buffer[4]);
gyro_out_Z=((sensor_buffer[5]<<8)+sensor_buffer[6]);
acc_out_X=((sensor_buffer[7]<<8)+sensor_buffer[8]);
acc_out_Y=((sensor_buffer[9]<<8)+sensor_buffer[10]);
acc_out_Z=((sensor_buffer[11]<<8)+sensor_buffer[12]);
HMC_xo=((sensor_buffer[13]<<8)+sensor_buffer[14]);
HMC_yo=((sensor_buffer[15]<<8)+sensor_buffer[16]);
HMC_zo=((sensor_buffer[17]<<8)+sensor_buffer[18]);
adc_pressure=(((long)sensor_buffer[19]<<16)+(sensor_buffer[20]<<8)+sensor_buffer[21]);

这条线是干什么的:

variable = (array_var<<8) + next_array_var

它对8位有什么影响?

<<8  ?

更新:有其他语言的例子吗(java,处理)?

处理示例:(为什么使用类似H的标头?)。

/*
 * ReceiveBinaryData_P
 *
 * portIndex must be set to the port connected to the Arduino
 */
import processing.serial.*;
Serial myPort;        // Create object from Serial class
short portIndex = 1;  // select the com port, 0 is the first port
char HEADER = 'H';
int value1, value2;         // Data received from the serial port
void setup()
{
  size(600, 600);
  // Open whatever serial port is connected to Arduino.
  String portName = Serial.list()[portIndex];
  println(Serial.list());
  println(" Connecting to -> " + Serial.list()[portIndex]);
  myPort = new Serial(this, portName, 9600);
}
void draw()
{
  // read the header and two binary *(16 bit) integers:
  if ( myPort.available() >= 5)  // If at least 5 bytes are available,
  {
    if( myPort.read() == HEADER) // is this the header
    {
      value1 = myPort.read();                 // read the least significant byte
      value1 =  myPort.read() * 256 + value1; // add the most significant byte
      value2 = myPort.read();                 // read the least significant byte
      value2 =  myPort.read() * 256 + value2; // add the most significant byte
      println("Message received: " + value1 + "," + value2);
    }
  }
  background(255);             // Set background to white
  fill(0);                     // set fill to black
// draw rectangle with coordinates based on the integers received from Arduino
  rect(0, 0, value1,value2);
}

您的代码具有相同的模式:

value = (partial_value << 8) | (other_partial_value)

数组的数据存储在8位字节中,但值存储在16位字节中。每个数据点都是两个字节,最高有效字节存储在数组的第一个字节中。这种模式简单地通过将最高有效字节向左移位8位,然后将最低有效字节"或"到较低的8位来构建完整的16位值。

它是一个移位运算符。它将变量中的位向左移动8。向左移动1位相当于乘2(向右移动除以2)。所以本质上<lt;8等于乘以2^8。

有关C++运算符及其作用的列表,请参见此处:http://en.wikipedia.org/wiki/C%2B%2B_operators

<<是左位移位运算符,结果是第一个操作数向左移动的位,从右边填充0位。

伪代码中的一个简单示例:

x = 10000101;
x = x << 3;
now x is "00101000"

学习维基百科上的Bitwise操作文章进行介绍。

这只是一个位移位运算符。如果基本上是取值并将位向左移位。这相当于将该值乘以2^8。该代码看起来像是读取数组的2个字节,并从每对中创建一个16位整数。

似乎sensor_buffer是一个字符矩阵。为了得到你的值,例如gyro_out_X,你必须将sensor_buffer[1]sensor_buffer[2]结合起来,其中

  • sensor_buffer[1]保存最高有效字节
  • sensor_buffer[2]保存最低有效字节

在这种情况下

int gyro_out_X=((sensor_buffer[1]<<8)+sensor_buffer[2]);

组合两个字节:

  • 如果sensor_buffer[1]为0xFF
  • CCD_ 10为0x10

则gyro_out_X为0xFF10

它将位向左移动8位,例如:

0000000001000100 << 8 = 0100010000000000
0000000001000100 << 1 = 
0000000010001000 << 1 =
0000000100010000 << 1 =
0000001000100000 << 1 =
0000010001000000 << 1 =
0000100010000000 << 1 =
0001000100000000 << 1 =
0010001000000000 << 1 =
0100010000000000