Reputation:
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?!
Upvotes: 1
Views: 409
Reputation:
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++;};
}
}
Upvotes: 0
Reputation: 1437
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.
Upvotes: 2