【问题标题】:How to calculate the nof of bits of 0 in bit stuffing?如何计算位填充中0的位数?
【发布时间】:2015-02-06 10:22:51
【问题描述】:

如果给定的字符串是01111,文本是0111101111,位填充所需的位数是多少?

【问题讨论】:

    标签: bit bitstuffing


    【解决方案1】:
    #include <iostream>
    #include <string>
    
    int main() {
        // read bits '0'/'1' into a string; terminate on any other char
        std::string data_stream ;
        char bit ;
        while( std::cin >> bit && ( bit == '0' || bit == '1' ) ) data_stream += bit ;
    
        // stuff a zero bit after four consecutive bits of the same value.
        std::string stuffed_stream ;
        int cnt = 0 ;
        char bit_last_seen = 0 ;
        int stuff_bits_added = 0 ;
    
        for each bit in the data_stream
        {
            if( it is the same as the previous bit ) increment cnt
    
        else // it is a different bit 
        {
            bit_last_seen := this bit ;
            cnt := 1 ; // restart count at 1
        }
    
        stuffed_stream += bit ; // add the bit 
    
            if( cnt == 4 ) // there are four consecutive bits of the same value
            {
                stuffed_stream += '0' ; // stuff with a zero bit
                cnt := 0 ; // and reset cnt to zero
                ++stuff_bits_added ; // increment the count of stuff bits added
            }
        }
    
        // print out the results  
        std::cout << "     data stream: " << data_stream << '\n'
               << "  stuffed stream: " << stuffed_stream << '\n'
               << "stuff bits added: " << stuff_bits_added << '\n' ;
    
    
        // TODO:
        // The de-stuffing code to process the stuffed data to recreate the original 
        // and verify that the original data is recovered correctly.           
    }
    

    来自http://www.cplusplus.com/forum/general/98631/#msg533723的一些伪代码

    现在您的问题似乎是什么?

    【讨论】:

      猜你喜欢
      • 2016-05-15
      • 2012-08-11
      • 2011-07-21
      • 1970-01-01
      • 2011-05-27
      • 2019-03-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多