1

I am reading over some code and I finally need to figure out how this function works. I understand what it is used for and why it is used, but further than that, its magic.

From what I understand the function takes in a value that had all the info compressed into it. So instead of having 16 integers that only hold the value 0 or 1, it packs each 0 or 1 value into the bits of the integer. And this function takes those bits out and puts them each into a char.

The function is called like so

DecompressInputs(DigOut[0], &input[64]);

With DigOut and input being an arrays defined as such

UWORD DigOut[2];
char input[NUM_INPUTS]; // NUM_INPUTS = 80

And the function itself

/*===============================================*/
/* 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;
    }
}
4

3 回答 3

0

这个版本的代码可能更容易理解,因为它不依赖于修改值:

static __inline__ void DecompressInputs(int bits, char *output)
{
    for (int bit = 0; bit < 16; ++bit)
    {
        int mask = 1 << bit;
        if (bits & mask)
            output[bit] = 0x0a5;
        else
            output[bit] = 0;
    }
}
于 2013-07-30T13:58:23.020 回答
0

该函数检查bits( if(bits & 0x0001)) 中的 LSB。如果设置了该位,则将第一个字符input设置为'\xa5'(可能是任何字符),否则设置为'\0. 然后input是 incemented,因此在下一个循环中,原始数组的第二个字符将被设置并bits移动 ( bits = bits >> 1;),因此下一个循环将检查原始值的第二个 LSB。执行 16 次以解压缩 16 位。

让我们举个例子:

位 = 0x0005(二进制 0000 0000 0000 0101)

那么我们有

  1. bits & 0x0001是真的 ==> 输入[0] = '\xa5'

    bits = bits >> 1==> 位 = 0x0002(二进制 ... 0010)

  2. bits & 0x0001为假 ==> input[1] = '\x00' (关于原始输入)

    bits = bits >> 1==> 位 = 0x0001(二进制 ... 0001)

  3. bits & 0x0001是真的 ==> 输入[2] = '\xa5'

    bits = bits >> 1==> 位 = 0x0000(二进制 ... 0000)

.. 等等

于 2013-07-30T12:11:20.690 回答
0

好的,让我们试着解释一下这里发生了什么。

首先,我认为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;
    }
}
于 2013-07-30T12:12:57.723 回答