我正在阅读一些代码,最后我需要弄清楚该函数的工作原理。我了解它的用途以及为什么使用它,但除此之外,它还具有魔力。
据我了解,该函数采用了一个将所有信息压缩到其中的值。因此,不是将16个仅包含值0或1的整数打包,而是将每个0或1值打包到该整数的位中。然后此函数将这些位取出,然后将它们每个都放入一个char中。
该函数的调用方式如下
DecompressInputs(DigOut[0], &input[64]);
在DigOut和input是这样定义的数组的情况下
UWORD DigOut[2];
char input[NUM_INPUTS]; // NUM_INPUTS = 80
和功能本身
/*===============================================*/
/* Name: DecompressInputs */
/* Purpose: Convert a 16 bit value into an array */
/* of 16 boolean values. */
/*===============================================*/
static __inline__ void DecompressInputs(int bits,char *input)
{
char i = 16;
while(i)
{
if(bits & 0x0001)
*input++ = 0x0a5;
else
*input++ = 0x000;
bits = bits >> 1;
i-=1;
}
}
好的,让我们尝试解释这里发生的事情。
首先,我认为static __inline__ void DecompressInputs(int bits,char *input)
应该更新为static __inline__ void DecompressInputs(int bits,char *output)
它,因为它看起来更像是输出值而不是输入值:但是,这很详细。
让我们尝试使其更加清晰:
static __inline__ void DecompressInputs(int bits,char *output)
{
char i = 16;
while(i)
{
/* This will be true if the lowest bit of bits is 1. Otherwise, this is false */
if(bits & 0x0001)
{
/* For whatever reason, you chose 0x0a5 to represent a bit whose value is 1. We write that value to the char pointed by output */
*output = 0x0a5;
/* Now that the value has been written, increment output so the next write will go the next char */
output++;
}
else
{
/* Same here, but we store 0x00 instead */
*output = 0x000;
output++;
}
// Now we bitshift bits, so the next time we do bits & 0x0001, we will check for the second bit of the original integer
bits = bits >> 1;
i-=1;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句