What is the best way to shift data by X bits into and out of a file?

独自空忆成欢 提交于 2021-02-20 05:13:18

问题


I have a dataset, made up of a mask file, and a data file. the mask file tells the decoder whether there are 8 bits per field present, or 4, for the current offset of the datafile. I need to shift the data out according to the mask, and write the decoded file, with all 8 bits per field. I'm trying to accomplish this in C.

void shift_4bits_left(unsigned char* array, unsigned short size)
{
int i;
unsigned char shifted = 0x00;    
unsigned char overflow = (0xF0 & array[0]) >> 4;

    for (i = (size - 1); i >= 0; i--)
    {
    shifted = (array[i] << 4) | overflow;
    overflow = (0xF0 & array[i]) >> 4;
    array[i] = shifted;
    }
}

in function:

while(len>count){
        //count=0;
        if(bit==0)yy=fread(blockchar,1,len,fp1);
        if(bit==0)memcpy(blockchar2,blockchar,len);
        count+=yy;
        memset(outputbuf,0,64);
        for(x=0;x<len;x++){
        count2=0;
        //count2=1;

        if(sometests(blockchar[x])==1)
        {
          shift_4bits_left(&blockchar[x],(yy-(bittest/2))+1);
          count2=1;
          total++;
          if(total%2==0)len--;
        }

//set bit for current position in mask file's buffer
        if((x)%8 == 0)outputbuf[x]+=(count2<<7);
        if((x)%8 == 1)outputbuf[x]+=(count2<<6);
        if((x)%8 == 2)outputbuf[x]+=(count2<<5);
        if((x)%8 == 3)outputbuf[x]+=(count2<<4);
        if((x)%8 == 4)outputbuf[x]+=(count2<<3);
        if((x)%8 == 5)outputbuf[x]+=(count2<<2);
        if((x)%8 == 6)outputbuf[x]+=(count2<<1);
        if((x)%8 == 7)outputbuf[x]+=count2;
        }

The problem is this is very slow when working with large datasets, I would prefer to make this iterative over the file, without storing the files in arrays.


回答1:


I solved it by looking at 256 bytes at a time, and when not shifting, using memcpy. Thanks for the help guys.



来源:https://stackoverflow.com/questions/12568798/what-is-the-best-way-to-shift-data-by-x-bits-into-and-out-of-a-file

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!