English 中文(简体)
左轮
原标题:left shift with or
  • 时间:2011-11-21 08:07:13
  •  标签:
  • c
i2c_receiveData(sDevice *psDevice, byte_t *pbBuffer, uint16_t *puiLen)
{
.
.    
//extract the packet data length
unFrameLen = (*(pbBuffer+1) << 8) | *(pbBuffer + 2);
if(unFrameLen > *puiLen)
unFrameLen=*puiLen;
.
.
}

在这里,这一说法是如何找到Fileo Length?

unFrameLen = (pbBuffer+1) << 8) ∗(pbBuffer + 2);

http://strong>pbBuffer是没有签名的果园。

calling function was,

i2c_receiveData(psDevice, prgDataRecv, &unRegLen);
最佳回答

在这种情况下,“框架长度”似乎被储存在经过的缓冲地带。

It also appears that it is a 16-bit integer.

In order to get a usable 16-bit integer you must unpack from the buffer. It would be better to cast and use htons/ntohs instead, but I presume the architecture is well-known and portability isn t a concern.

举例来说,pbBuffer ={0, 1, 2},这最终是:

(1 << 8) | 2;

......

(00000001b << 8) | 00000010b

......轮式<代码>1b 左轮8 借方:

100000000b

... and OR-ing with 10b:

100000010b

现在,在<代码>pbBuffer[1..2]上的2个8个轨道分类账中,有16个轨道分类:

100000010b = 0x102
问题回答

显然,议定书第2和第3条按批次发送了时限。 这种表达方式使2个 by子的16倍感愤怒。

*(pbBuffer+x)pbBuffer[x]相同,因此第1号星号在8个班前排出,第2号星号在外加。

如果您在<条码>pbBuffer上收到,例如<条码>{0 0000, 0101 0101, 1100 1100, ......} (binary), 两条经提取的正本

*(pbBuffer+1) = 0101 0101
*(pbBuffer+2) = 1100 1100

计算方法

 *(pbBuffer+1) << 8                  = 0101 0101 0000 0000
(*(pbBuffer+1) << 8) | *(pbBuffer+2) = 0101 0101 1100 1100

借重或(>>>与此处添加相同,因为8个下限的<代码>(*(pbBuffer+1) <<8)均为0。

请注意,细节视平台而定。

发言分为小部分:

 *(pbBuffer+1)       /* gets the byte_t value one beyond the pbBuffer pointer */
(*(pbBuffer+1) << 8) /* shifts that byte eight bits left -- in essence
                        xxxxxxxx00000000 -- where xxx comes from pbBuffer+1 */


*(pbBuffer+2)        /* gets the byte_t value two beyond the pbBuffer pointer */

(*(pbBuffer+1) << 8) | *(pbBuffer + 2)
/* xxxxxxxx00000000  |  yyyyyyyy */
   xxxxxxxxyyyyyyyy */

This reconstructs the two bytes specified into a single 16-bit data type. These sorts of operations are especially common when dealing with networking code, because different machines have different byte orders. The different orders mean numbers larger than one byte can t be easily passed around on networks without use of a network byte order to serve as an intermediary format. (Some computers work in network byte order natively while others must perform conversions all the time.)





相关问题
Fastest method for running a binary search on a file in C?

For example, let s say I want to find a particular word or number in a file. The contents are in sorted order (obviously). Since I want to run a binary search on the file, it seems like a real waste ...

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

Tips for debugging a made-for-linux application on windows?

I m trying to find the source of a bug I have found in an open-source application. I have managed to get a build up and running on my Windows machine, but I m having trouble finding the spot in the ...

Trying to split by two delimiters and it doesn t work - C

I wrote below code to readin line by line from stdin ex. city=Boston;city=New York;city=Chicago and then split each line by ; delimiter and print each record. Then in yet another loop I try to ...

Good, free, easy-to-use C graphics libraries? [closed]

I was wondering if there were any good free graphics libraries for C that are easy to use? It s for plotting 2d and 3d graphs and then saving to a file. It s on a Linux system and there s no gnuplot ...

Encoding, decoding an integer to a char array

Please note that this is not homework and i did search before starting this new thread. I got Store an int in a char array? I was looking for an answer but didn t get any satisfactory answer in the ...

热门标签