Rice Decoding running time problem

Hi all, currently, I write a little program with Rice coding.

When I Encode, it runs quickly, but when Decode, it runs very-very slow.

Can some one help me to fix this?

this is my source code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
#include <iostream>
#include <fstream>
#include <string>
#include <bitset>
#include <ctime>
#include <algorithm>
#include "math.h"

using namespace std;

//int source[]={76,56,94,12,91,23,52,98,16,23,42,12,57,39,90,52,46,4,72,27,93};
int source[50];
unsigned char komp[100];
int dekomp[50];

const unsigned long bit_mask[] = {
    0x00000000, 0x00000001, 0x00000003, 0x00000007,
    0x0000000f, 0x0000001f, 0x0000003f, 0x0000007f,
    0x000000ff, 0x000001ff, 0x000003ff, 0x000007ff,
    0x00000fff, 0x00001fff, 0x00003fff, 0x00007fff,
    0x0000ffff, 0x0001ffff, 0x0003ffff, 0x0007ffff,
    0x000fffff, 0x001fffff, 0x003fffff, 0x007fffff,
    0x00ffffff, 0x01ffffff, 0x03ffffff, 0x07ffffff,
    0x0fffffff, 0x1fffffff, 0x3fffffff, 0x7fffffff,
    0xffffffff
};

bool GetBit( unsigned char ch, int i ) 
{
   unsigned char mask = 1 << i;
   return mask & ch ;   
}
int countBit()
{
	unsigned long numBit=0, tes=0, count=0, total=0, mean=0;
	short tempInput;
	for(unsigned int i=0;i<20;i++)
	{
		tempInput = source[i];
		if(tempInput<0)
			tempInput = tempInput*-1;
		total = total + tempInput;
	}
	mean = log2(ceil(total/=20));
	return mean;
}

int RiceEncode(int M)
{
	int num, bits=0, count=0, rem;
	int c=0, sign;
	unsigned long code=0, temp=0;
	cout<<"q: ";
	for(int i=0;i<20;i++)
	{
		num = source[i];
		temp = source[i];
		if(num<0)
		{
			num = -1 * num;
			sign= -1;
		}
		else
			sign=1;
		int q = num>>M;
		cout<<q<<", ";
		rem = num&bit_mask[M];
		code = code|(bit_mask[q]<<bits);	// unary
		bits=bits+q;
		while(bits>=8)
		{
			komp[count]=code&bit_mask[8];
			code=code>>8;
			bits-=8;
			count++;
		}
		bits++;								// terminator
		for(int j=0;j<M;j++)				// binary
		{
			code=code|((rem&1)<<bits);
			
			bits++;
			rem=rem>>1;
			while(bits>=8)
			{
				komp[count]=code&bit_mask[8];
				code=code>>8;
				bits-=8;
				count++;
			}
		}
		if(sign<0)						// sign negatif
		{
			code = code | (1<<bits);
			bits++;
		}
		else							// sign positif
			bits++;
		while(bits>=8)
		{
			komp[count]=code&bit_mask[8];
			code=code>>8;
			bits-=8;
			count++;
		}
	}
	while(bits>0)
	{
		komp[count]=code&bit_mask[8];
		code=code>>8;
		bits-=8;
		count++;
	}
	return count;
}

void RiceDecode(int M, int Count)
{
	int q = 0, a=0, count=0;
	int bitC=0, tempRem=0;
	int v=0, proses=0, sign;
	bool flag=false, bitFlag=false;
	cout<<endl;
	cout<<"q: ";
	for(int i=0;i<Count;i++)
	{
		bitC=0;
		//cout<<" "<<int(komp[i])<<" ";
		while(bitC<8)
		{
			if(bitFlag==true)
			{
				if(GetBit(komp[i],bitC)==true)
					sign= -1;
				else
					sign= 1;
				dekomp[count]=((q<<M)+tempRem)*sign;
				a=0;
				q=0;
				tempRem=0;
				count++;
				flag=false;
				bitFlag=false;
			}
			else
			{
				if(flag==false)
				{
					if(GetBit(komp[i],bitC)==true)
						q++;
					else
					{
						cout<<q<<", ";
						flag=true;
					}
				}
				else
				{
					
					tempRem=tempRem+(GetBit(komp[i],bitC)<<a);
					a++;
					if(a==M)
					{
						/*dekomp[count]=(q<<M)+tempRem;
						a=0;
						q=0;
						tempRem=0;
						count++;
						flag=false;*/
						bitFlag=true;
					}
				}
			}
			bitC++;
		}
	}
	cout<<"\ncount: "<<count;
}

int main()
{
	int iSecret, iGuess, parameter;
	ofstream fout;
	//fout.open("/home/crayon66/Data/tesRand", ios::binary);
	//char code_write;

	srand ( time(NULL) );
	cout<<"A: ";
	for(int i=0;i<20;i++)
	{
		iSecret = rand() % 100 - 50;
		source[i]=iSecret;
		cout<<source[i]<<", ";
	}
	cout<<endl;
	parameter=countBit();
	cout<<"parameter: "<<parameter<<endl;
	iGuess=RiceEncode(parameter);
	cout<<"\nK: ";
	for(int i=0;i<iGuess;i++)
	{
		cout<<int(komp[i])<<", ";
		//printf("%0x, ",char(komp[i]));
	}
	RiceDecode(parameter, iGuess);
	cout<<"\nD: ";
	for(int i=0;i<20;i++)
	{
		cout<<int(dekomp[i]);
		cout<<", ";
		//printf("%i, ",dekomp[i]);
	}
	
	return 0;
}


thanks for your help.
Please don't spam forums. See my response in General C++.
Thanks for your advice Duoas, surely i don't wanna spam forums.

i write that code in linux and i don't know exactly where should i ask for my problem.

anyway, thanks. and i still can't solve this problems.

or anyone have any idea to read bits quickly? for example, read 5 bits or 15 bits.

thanks
Topic archived. No new replies allowed.