2012-07-23 171 views
4

我想實現位填充爲我工作的一個項目,即一個簡單的軟件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,因此必須確保,無論是標頭也不是數據包含連續的6人。這可以通過比特填充完成,例如5對那些

11111111 
converts to 
111110111 

11111000 
converts to 
111110000 

現在,如果我要有效地實現這一點,我想我不應該使用1和0,在這裏我要的數據字節轉換爲1和0陣列的每個序列之後插入一個零,然後填充一個數組等等,但是靜態大小的位域似乎也不適合,因爲由於位填充,內容的長度是可變的。

我可以使用哪些數據結構,更有效地做到位餡?

+0

我剛纔看到這一點。如果你還沒有做到這一點,你需要一個數據結構或算法來做到這一點? – 2013-04-25 17:55:03

回答

1

我剛看到這個問題,現在看到這是沒有答案,而不是刪除,我會繼續和答案。它可能會幫助其他人看到這個問題並提供關閉。

位填充:這裏的1's最大連續序列是551's後應該有那些51'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("%llx\n",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 
} 

的問題是,如果有10個5的更多的連續的1,那麼它可能溢出。最好分開然後再重複一遍。

+0

感謝您的回答!我其實是用循環緩衝區去的。由於保密協議,我很不幸地不能顯示來源:( – 2013-04-26 06:34:16

+0

多數民衆贊成在我只是想知道爲什麼沒有人回答這樣一個有趣的問題,乾杯! – 2013-04-26 06:42:33

相關問題