简体   繁体   中英

Discriminate bits after “bit stuffing”

I have written a piece of code to add a '0' after 6 consecutive '1' in a bit stream. 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 '.)

As you can see a '0' was added after each '111111' and to retrieve the original stream one has to remove it. 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. 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. 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++;};
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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