C ++代码说明

时间:2012-09-11 22:03:20

标签: c++ bitwise-operators

我有来自串行读取的数组,名为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);
}

6 个答案:

答案 0 :(得分:10)

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

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

您的数组以8位字节存储数据,但值为16位字节。每个数据点都是两个字节,最重要的字节首先存储在数组中。该模式通过将最高有效字节8位向左移位,然后将最低有效字节向左移位到低8位,简单地构建完整的16位值。

答案 1 :(得分:4)

它是一个班次运营商。它将你变量中的位向左移动8.向左移1位相当于乘以2(向右移位除以2)。因此,基本上&lt;&lt; 8相当于乘以2 ^ 8。

请在此处查看C ++运算符列表及其功能: http://en.wikipedia.org/wiki/C%2B%2B_operators

答案 2 :(得分:3)

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

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

x = 10000101;
x = x << 3;

now x is "00101000"

研究维基百科上的Bitwise operation文章,了解相关信息。

答案 3 :(得分:2)

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

答案 4 :(得分:2)

似乎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
  • sensor_buffer[2]是0x10

然后gyro_out_X是0xFF10

答案 5 :(得分:1)

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

0000000001000100 << 8 = 0100010000000000

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