将数组连接到位

时间:2018-09-15 11:09:39

标签: c concatenation shift

如何将低密度数组data的1和0串联到较小的更密集填充的数组c

uint8_t data[16] = {1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1};
uint8_t c[2] = {0};
/*  desired result
    c[0] = 11011011
    c[1] = 10001101 
*/

到目前为止,我在这里有点挣扎,我已经知道了,但是它似乎并没有达到我的预期:

static void compress(unsigned char *out, unsigned char *in, int length)
{
    for(int i=0; i<length; i++)
    {
        if((i+1)%9==0)
            out++;
        *out |= in[i]<<((length-i)%9);

    }
}  
int main(){
    compress(c,data,16);
    printf("%d",c[0]); //should be 219
    printf("%d",c[1]); //should be 177 (edit)
}

谢谢你帮助我!

3 个答案:

答案 0 :(得分:2)

static void compress(uint8_t *out, uint8_t *in, size_t length)
{
    memset(out, 0, length >> 3 + !!(length & 7));
    for(size_t i = 0; i < length; i++)
    {
        out[i >> 3] |= (in[i] << (7 - (i & 7)));
        //out[i >> 3] |= ((!!in[i]) << (7 - (i & 7)));  - if array elements may be not only 0 or 1.
    }
}  

int main()
{
    uint8_t data[16] = {1, 1, 0, 1, 1, 0, 1, 1, 1, 0, 0, 0, 1, 1, 0, 1};
    uint8_t c[sizeof(data) >> 3 + !!(sizeof(data) & 7)];

    compress(c,data,16);
    printf("%d\n",c[0]); //should be 219
    printf("%d\n",c[1]); //should be 141
}

答案 1 :(得分:1)

在代码printf ("%d %d\n", i,(length-i)%9 );中添加一行,即可看到问题所在。

不使用取模运算,添加保留位移数(初始值为7)的变量,并在其为负数时将其重置:

static void compress2(unsigned char *out, unsigned char *in, int length)
{
    int shift = 7;
    for(int i=0; i<length; i++)
    {
        *out |= in[i] << shift;
        if (--shift < 0)
        {
            ++out;
            shift = 7;
        }
    }
}

答案 2 :(得分:1)

unit8_t的大小仅为8位,而不是9位。因此,从根本上讲,错误出在%9而不是%8。


您可以使用单独的计数器(b),将移位量从7降低到0。一旦达到-1,请前进。以(length-1)开头,因为对于长度1,您希望它为最低位,即偏移量0。

static void compress(unsigned char *out, unsigned char *in, int length)
{
    for(int* p=in, int b=(length-1)%8; p<in+length; p++, b--)
    {
        if(b < 0)
        {
            out++;
            b = 7;
        }
        *out |= *p << b;
    }
}  

您还可以使用类似out [(length-i-1)/ 8] | = in [i] <<(length-i-1)%8


P.S。位顺序取决于平台...