简体   繁体   English

这个pData [1 + 2 * i] << 8 | pData [2 + 2 * i] C ++语法是什么意思?

[英]What is the meaning of this pData[1+2*i]<<8|pData[2+2*i] C++ syntax?

what is the meqaning of pData[1+2*i]<<8|pData[2+2*i] where pData[ ] is the array containing BYTE data? 什么是pData[1+2*i]<<8|pData[2+2*i]的meqaning,其中pData[ ]是包含BYTE数据的数组? I have the following function in the main function 我在main函数中有以下功能

{
..........
....
BYTE Receivebuff[2048];
..
ReceiveWavePacket(&Receivebuff[i], nNextStep);
....
...
..
}

Where Receivebuff is the array of type BYTE. 其中Receivebuff是BYTE类型的数组。

ReceiveWavePacket(BYTE * pData, UINT nSize)
{
 CString strTest;
 for(int i = 0 ; i < 60 ; i++)
 {
  strTest.Format("%d\n",(USHORT)(pData[1+2*i]<<8|pData[2+2*i]));
  m_edStatData.SetWindowTextA(strTest);
          }
}

I want to know the meaning of " ,(USHORT)(pData[1+2*i]<<8|pData[2+2*i]) . 我想知道“ ,(USHORT)(pData[1+2*i]<<8|pData[2+2*i])的含义。

Can any body please help me? 任何人都可以帮帮我吗?

This seems to be code for synthesizing a 16-bit value out of two eight-bit values. 这似乎是用于从两个八位值合成16位值的代码。 If you'll note, the math has the form 如果你注意,那么数学就有了表格

(a << 8) | b

For suitable a and b. 适用于a和b。 This first part, (a << 8), takes the eight bits in a and shifts them up eight positions, giving a 16- bit value whose first eight bits are the bits from a and whose second eight bits are all zero. 第一部分(a << 8)取a中的8位并将它们向上移动8个位置,得到16位值,其前8位是来自a的位,其后8位全部为0。 Applying the bitwise OR operator between this new value and the value of b creates a new sixteen-bit value whose first eight bits are the bits of a (because zero-extending b for the OR step leaves these bits intact) and whose lower eight bits are the bits of b, since ORing zero bits with the bits of b yields b. 在这个新值和b的值之间应用按位OR运算符会创建一个新的16位值,其前8位是a的位(因为OR步骤的零扩展b使这些位保持不变)并且其低8位是b的位,因为OR的零位与b的位产生b。

I think @templatetypedef is correct, it does look like code that creates a 16-bit value by logical OR'ing two 8 bit values. 我认为@templatetypedef是正确的,它看起来像通过逻辑或两个8位值创建一个16位值的代码。 ( since it's OR'ing two elements from your array of BYTE's ). (因为它是你的BYTE数组中的两个元素)。

Logical OR'ing of two bits basically means, if either of the bits is 1 then the result is 1 ("this bit OR that bit"). 两位的逻辑“或”基本上意味着,如果任一位为1则结果为1(“该位或该位”)。

As a further example, take a look at this function that takes a pointer to a char (that is 8 bits in size), and returns an integer ( in this implementation that means 32 bits). 作为另一个例子,看看这个函数,它接受一个指向char(大小为8位)的指针,并返回一个整数(在这个实现中,这意味着32位)。

int Read32(const char *pcData)
{
    return ( (pcData[3]<<24) & 0xff | pcData[2]<<16 | pcData[1]<<8 | pcData[0]);
}

If pcData is a pointer to an array of char - then it takes the 3rd char it finds and it shifts it by 24 bits: 如果pcData是一个指向char数组的指针 - 那么它会找到它找到的第3个char并将它移动24位:

eg 例如

if pcData[3] was 10110001 then it is now 如果pcData[3]是10110001那么现在就是

10110001000000000000000000000000

It takes this 32 bit value and subsequently OR's it with pcData[2], shifted by 16 bits - which means that if pcData[2] is 11111111 then the 32 bit value is now: 它需要这个32位值,然后将它与pcData [2]进行OR运算,移位16位 - 这意味着如果pcData[2]11111111则32位值现在为:

10110001111111110000000000000000

and so on with pcData[1] and pcData[0] . 依此类推pcData[1]pcData[0]

@templatetypedef's answer is correct, but there is another interesting thing going on here: @ templatetypedef的答案是正确的,但还有另一个有趣的事情:

(pData[1+2*i]<<8|pData[2+2*i])

i goes from 0 to 59, so this will process 60 words, starting with pData[1] (1+2*0 == 1). i从0到59,所以这将处理60个单词,从pData[1] (1 + 2 * 0 == 1)开始。
This means that the first byte in the array is never processed, which seems odd. 这意味着数组中的第一个字节永远不会被处理,这似乎很奇怪。 Why isn't this the more natural pData[2*i]<<8|pData[2*i+1] ? 为什么这不是更自然的pData[2*i]<<8|pData[2*i+1]

One possibility: Word data can be stored in a byte stream 2 ways: the word 0xAA11 can be stored as 0xAA 0x11 or 0x11 0xAA. 一种可能性:字数据可以以2种方式存储在字节流中:字0xAA11可以存储为0xAA 0x11或0x11 0xAA。 Imagine it is the latter. 想象一下,这是后者。

For the words : 0xAA11 0xBB22 0xCC33 ... 对于单词: 0xAA11 0xBB22 0xCC33 ...
The byte stream will be 11 AA 22 BB 33 CC ... 字节流将是11 AA 22 BB 33 CC ...

Parsing with the 'natural' method would give 0x11AA 0x22BB... , which is obviously wrong. 使用'自然'方法解析将给出0x11AA 0x22BB... ,这显然是错误的。

This code will print 0xAA22 0xBB33, 0xCC44 .... , which will probably pass a quick-look sanity check, but is actually totally incorrect. 此代码将打印0xAA22 0xBB33, 0xCC44 .... ,这可能会通过快速查看完整性检查,但实际上完全不正确。

I hope the extra +1 wasn't added to "fix" the endianness issue. 我希望额外的+1不会被添加到“修复”字节序问题。

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

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