C++对代码的解释
C++ Explanation of the code
我有一个串行读取的数组,名为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
相关文章:
- 计算每个节点的树高,帮助我解释这个代码解决方案
- 有人能为我解释一下C++代码吗
- 请解释字谜的代码,我看不懂计数器数组,每个值已经是0
- 有人可以解释一下这段代码如何能够反转字符串
- 有人可以向我解释一下这段代码的作用吗?
- 有人可以详细解释这个回文代码是如何工作的吗?
- 如何在 C++11 中解释静态代码
- 任何人都可以解释一下我是否需要 & 在第一个代码片段中
- 我不完全确定此代码的行为.有人可以向我解释一下吗?
- 有人可以解释我这个代码吗?它给出与我们输入的数字相反
- 我在 2D 数组的动态内存分配中遇到了一些奇怪的代码C++? 请解释一下这是什么?
- 解释以下宏代码的 O/P
- 如何解释 IDA Pro 生成的这些代码?
- 交叉和计算,任何人都可以解释代码吗?
- 请解释代码块"for(auto &it : iostr )"
- 需要解释C++代码的行为
- 解释C++代码
- 二叉搜索树的最低公共祖先解释代码
- 任何人都解释C++代码行
- 需要知道为什么代码以这种方式行为- C中的解释代码