how decoding works in this code

Hi,
i have this piece of code which is decoding function for rice algorithm. which i pass as input is a binary file, which is encoded.
working of encoder:
the range of input in encoding fn is 0-255, and the encoder creates a combination of unary and binary, for example if i have 42, it is encoded as (010)011111- for the part in bracket it is 42 mod 8 written in binary, part except bracket is int(42/8) written in unary.the in bitfile the value 010011111 is written.

now when i decode the file, i read bit by bit from the file, and the following fn is used which i didnt coded but got online:
here k=3, which is the number of bits for remainder.

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
/* decode input file */
    while ((bit = BitFileGetBit(bfpIn)) != EOF)
    {
        if (1 == bit)
        {
            byte++; //for ex. for 010011111, this part will read 5 1's, byte=00000101
             
        }
        else
        {
            /* finished unary portion */
            tmp = byte << k; //tmp=00101000 ( 00000101 << 3)
       ;
 
            if (EOF == BitFileGetBits(bfpIn, &byte, k))
            {
                /* unary was actually spare bits */
                break;
            }
 
            byte >>= (CHAR_BIT - k);        /* leftt justify bits, byte= 00000000 */
       
            byte |= tmp;// byte=00101000
        
            fputc(byte, fpOut);//here we write 40 instead of 42? where are the remaining 3 bits? i dont get this
 
            byte = 0;
        }
    }


As i understand the unary part is decoded, but i dont see where the binary part is decoded. i dont understand how this piece of code works with input from the file. I have written for each of the decoding steps, the value for tmp and byte in the code i thought, but they are not right bc i am getting correct decoded values when i run the code, so i am not understanding some part of it. plz can anyone help me understand this code.
Topic archived. No new replies allowed.