我应该使用哪种数据结构来填充位



我正试图为我正在进行的一个项目实现比特填充,即一个简单的软件AFSK调制解调器。简化的协议看起来像这样:

0111 1110    # burst sequence
0111 1110    # 16 times 0b0111_1110
   ...
0111 1110
   ...
   ...       # 80 bit header (CRC, frame counter, etc.)
   ...
0111 1110    # header delimiter
   ...
   ...       # data
   ...
0111 1110    # end-of-frame sequence

现在,我需要在接收到的数据中找到保留序列0111 1110,因此必须确保报头和数据都不包含六个连续的1s。这可以通过比特填充来完成,例如,在五个CCD_2s:的每个序列之后插入一个零

11111111  

转换为

111110111  

11111000  

转换为

111110000

如果我想有效地实现这一点,我想我不应该使用1s和0s的数组,在这些数组中,我必须将数据字节转换为1s和0s,然后填充数组等。但静态大小的位字段似乎也不适合,因为由于位填充,内容的长度是可变的。

我可以使用哪种数据结构来更有效地进行比特填充?

我刚刚看到这个问题,看到它没有得到回答,也没有被删除,我会继续回答。它可能会帮助其他看到这个问题的人,并提供结束语。

比特填充:这里1's的最大连续序列是5。在5 1's之后,应该在这些5 1's之后附加一个0

这是实现这一点的C程序:

#include <stdio.h>
typedef unsigned long long int ulli;
int main()
{
    ulli buf = 0x0fffff01, // data to be stuffed
         temp2= 1ull << ((sizeof(ulli)*8)-1), // mask to stuff data
         temp3 = 0; // temporary  
    int count = 0; // continuous 1s indicator
    while(temp2)
    {
        if((buf & temp2) && count <= 5) // enter the loop if the bit is `1` and if count <= 5
        {
            count++;
            if(count == 5)
            {
                temp3 = buf & (~(temp2 - 1ull)); // make MS bits all 1s
                temp3 <<= 1ull; // shift 1 bit to accomodeate the `0`
                temp3 |= buf & ((temp2) - 1); // add back the LS bits or original producing stuffed data
                buf = temp3;
                count = 0; // reset count
                printf("%llxn",temp3); // debug only               
            }           
        }
        else
        {
            count = 0; // this was what took 95% of my debug time. i had not put this else clause :-)
        }
        temp2 >>=1; // move on to next bit.
    }
    printf("ans = %llx",buf); // finally
}

这样做的问题是,如果连续5个1中有10个以上,那么它可能会溢出。最好先分开,然后再重复。

最新更新