Jump to content
Sign in to follow this  
linuxuser

Programming Question

Recommended Posts

I have a small programming question. It is more like C/C++ programming question. I am trying to write a function in C or C++ that allows me to write a 9 bit value to a file. Can anyone help me with this. Psuedocode or code will be helpful. smile.gif

 

Share this post


Link to post
Share on other sites

QUOTE (rs_1915 @ Feb 25 2003, 04:02 AM)
I have a small programming question. It is more like C/C++ programming question. I am trying to write a function in C or C++ that allows me to write a 9 bit value to a file. Can anyone help me with this. Psuedocode or code will be helpful. smile.gif

Dear rs_1915,

 

Can you elaborate little bit more your problem. just saying to write 9 bit value to file is not much clear. It would be easy to understand, if you can give some example. like input and output.

 

regards,

Share this post


Link to post
Share on other sites

Thanks guys for the reply.

Sanskar..let me try to explain the problem.

In C/C++ , u can either write a byte ( which is char = 1 byte or 8 bits), or u can write 2 bytes ( word), or u can write int ( which is 4 bytes) in x86 machines.

u can use putc( char, FILE) and write a char variable ( again it is 1 byte or 8 bits), or use fwrite( buffer, sizeof( unsigned int) , 1, FILE) and write 1 byte, 2 bytes, or 4 bytes.

but there is not any function in ANSI C/C++ which can write 9 bits...........8 bits + 1 bit.

so my question is how do u write it?

smile.gif

Share this post


Link to post
Share on other sites

Sanskar,,,thanks for the help!!

I found the solution and decided to post it here. ( may be somebody in future is looking for similar type of solution).

I also encourage all the nepalese students studying computer science to post their problems if they have any ( software or hardware).

 

Ok in theory, there are two solutions.

1)we know that we can write1 byte or 2 bytes or 4 bytes. since 9 bits value is has 1 bit more than a byte( 8 bits), so try to convert 9 bits value to word( 2 bytes) or int( 4 bytes). the way of converting is, we can just pad 0 byte at the end of 9 bits and make it 2 bytes of 4 bytes

eg. 10101010 1 ( 9 bits value)

becomes 10101010 10000000 ( 2 bytes).

2) Another solution (better one) if u want to pack is..

write only 8 bits out of 9 bits...save 1 bit and add that 1 bit with next 9 bits. now again write 8 bits from this new 9 bits value,,,now u have 2 bits extra..add that with other new 9 bits...and so on.

code for this is here...

 

output(FILE *file,unsigned int value)

{

static int bit_count=0;

static unsigned long bit_buffer=0L;

bit_buffer |= (unsigned long) value << (32-9-bit_count);

bit_count += 9;

while (bit_count >= 8)

{

putc(bit_buffer >> 24,file);

bit_buffer <<= 8;

bit_count -= 8;

}

}

 

 

Share this post


Link to post
Share on other sites

thanks rs_1915 for code and explanation. hope it will be helpful to other also. if you have any interesting problem/solution, then please do share here. hope other will also do same.

 

best regards,

Share this post


Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
Sign in to follow this  

×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.