简体   繁体   English

在“位填充”之后区分位

[英]Discriminate bits after “bit stuffing”

I have written a piece of code to add a '0' after 6 consecutive '1' in a bit stream. 我编写了一段代码,在位流中连续6个“ 1”之后添加了“ 0”。 But how to decode it? 但是如何解码呢?

Here an example of one bits stream: 这是一个比特流的示例:

original = {01101111110111000101111110001100...etc...}

stuffed  = {011011111O101110001011111O10001100...etc...}

(The ' O ' stand for the stuffed ' 0 '.) (“ O ”代表填充的“ 0 ”。)

As you can see a '0' was added after each '111111' and to retrieve the original stream one has to remove it. 如您所见,在每个“ 111111”之后添加了“ 0”,要检索原始流,必须将其删除。 Easy. 简单。

But... What if the original stream had the same form as the stuffed one? 但是...如果原始流与填充的流具有相同的形式怎么办? How do I know if I have to remove these bits?! 我怎么知道是否必须删除这些位?

I think you are confused with the basics. 我认为您对基本知识感到困惑。 Pretend you want a B added after 2 As. 假设您要在2 As之后添加B。 This is not 'stuffed': 这不是“塞满”的:

AAAAA

'Stuffing' it gives you: “填充”可以为您提供:

AABAABA

The above is either 'stuffed' or 'not stuffed'. 以上是“填充”或“未填充”。 In other words you can stuff it again: 换句话说,您可以再次填充它:

AABBAABBA

Or you could 'unstuff' it: 或者您可以“取消填充”它:

AAAAAA

What if the original stream had the same form as the stuffed one? 如果原始流与填充的流具有相同的形式怎么办?

So if a bitstream has 10 consecutive 1s in it then it has clearly not been stuffed. 因此,如果一个比特流中有10个连续的1,那么它显然没有塞满。 You can't say the same for a bitstream that could have been stuffed. 对于可能已经塞满的比特流,您不能说同样的话。

My question was so dumb... But it was late ! 我的问题太蠢了……可是来晚了!

Here the piece of code I wrote. 这是我编写的代码。 It takes two streams of bits. 它需要两个比特流。 The length of the stream to be stuffed is in its first byte. 要填充的流的长度在其第一个字节中。 It works well except the new length after stuffing is not yet updated. 除填充后的新长度尚未更新以外,它都可以正常工作。 I used macro so it's more readable. 我使用了宏,因此更具可读性。

#include    "bitstuff.h"
#include    <stdio.h>
#include    <stdlib.h>
#include    <inttypes.h>
#define     sbi(byte, bit)  (byte = byte  |  (1 << bit))
#define     cbi(byte, bit)  (byte = byte & ~ (1 << bit))
#define     ibc(byte, bit)  (~byte & (1 << bit))
#define     ibs(byte, bit)  (byte & (1 << bit))
#define     clr(byte)       (byte = 0)

void    bitstuff(uint8_t* stream, uint8_t* stuff) {

        int8_t      k = 7, b = 7;
        uint8_t     row = 0;
        uint8_t    len = 8**stream++;

        stuff++;

        while(len--) {

            if(ibs(*stream, k--)) {

                row++;

                if(row==5) {

                    cbi(*stuff, b--);
                    if(b<0) {b=7; stuff++;};
                    sbi(*stuff, b--);
                    if(b<0) {b=7; stuff++;};
                }

                else {

                    sbi(*stuff, b--);
                    if(b<0) {b=7; stuff++;};
                }
            }
            else {

                clr(row);
                cbi(*stuff, b--);
                if(b<0) {b=7; stuff++;};
            }

        if(k<0) {k=7; stream++;};
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM