hi all , hope all are well. i have been stuck for a long time in this code below. i am not understanding what these two function does . i remembered you people. you might help me to understand what this two function does. ok, let me write the code first.... then question.

code 1

---------

here is a function..........

#define BITS 12 // globally declared

input_code(FILE *input)

{

unsigned int return_value;

static int input_bit_count=0;

static unsigned long input_bit_buffer=0L;

while (input_bit_count <= 24)

{

input_bit_buffer |=

(unsigned long) getc(input) << (24-input_bit_count);

input_bit_count += 8;

}

return_value=input_bit_buffer >> (32-BITS);

input_bit_buffer <<= BITS;

input_bit_count -= BITS;

return(return_value);

}

question >> i want to know what this function does ? can you explain little bit.

code 2

---------

here is another function but RELATED........

output_code(FILE *output,unsigned int code)

{

static int output_bit_count=0;

static unsigned long output_bit_buffer=0L;

output_bit_buffer |= (unsigned long) code << (32-BITS-output_bit_count);

output_bit_count += BITS;

while (output_bit_count >= 8)

{

putc(output_bit_buffer >> 24,output);

output_bit_buffer <<= 8;

output_bit_count -= 8;

}

}

question >> i want to know what this function does ? can you explain little bit about this.

IN FACT these two are the two function of a sigle main code. i am just calling these two function.i dont understand what these two functions are are doing.

your input are valueable to me. any kind of help is highly appreciated. waiting for response.

thanking you

The NEXT time the function is used, the static variables still exist, so input_bit_buffer contains the value left behind the last time the function was used, so does input_bit_count.

- input_bit_count=20, so the loop will be entered

- a byte is read, say 0E, shifted left 4 to form 00E0, and or-ed into input_bit_buffer, which now contains

10110000 11000000 11010000 00000000 = B0 C0 D0 00 -> 10110000 11000000 11010000 11100000 = B0 C0 D0 E0

- input_bit_count is set to 28 hence the loop stops

- return_value is set to the 12 highest bits of input_bit_buffer, i.e. 00000000 00000000 00001011 00001100 = 0B 0C

- input_bit_buffer is shifted left 12 bits, to form 00001101 00001110 00000000 00000000 = 0D 0E 00 00

- input_bit_count is set to 28 - 12 = 16

- and the computed return_value is returned (B 0C)

So 12 bits. Indeed they are read until EOF, although the test on EOF is not done in the function. If you look in the manual on getc(), you see that the constant EOF is returned on end of file. I'm not sure, but I think EOF is defined as -1, so the character read is effectively set to FF. When reconstructing the original data, this should be no problem if you know the original length of the file.

Indeed it is impossible to read 12 bits, since characters are 8-bits wide, and 4-bit computers don't exist any longer. I talked about variable-bit-length data, since the constant BITS is defined as 12. For other purposes, BITS could be set to any other value and the function would read that amount of bits from the stream. In this case, 8 and 16 bits are read to form 12-bit values.

Hope I explained it somewhat better now. My apologies :)