Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Buzz1982

hoe to read and write bits from/to the file in C

This topic is 5892 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

hi, i am making a compression program using huffman algo,for which i have to read and write on the file in the form of bits. stdio.h does not provide functions for reading and writing bits to a file. if any one knows the solution please reply(please explain in detail). also if someone knows a good site regarding huffman compression do refer it.

Share this post


Link to post
Share on other sites
Advertisement
You don''t read single bits from a file. That would take forever. Read 1 byte, or more, and then mask out the bits with binary operators.

To get the single bits, try something like this


unsigned int a = 0x000000A6; // ... 00000000 10100110

a & 0x01 = 0 // least significant bit
(a>>1) & 0x01 = 1
(a>>2) & 0x01 = 1
(a>>3) & 0x01 = 0
.
.
(a>>7) & 0x01 = 1


When writing, just do the opposite.

There is an excellent tutorial on Huffman here on GameDev:
http://www.gamedev.net/reference/articles/article286.asp

Regards Mats



Share this post


Link to post
Share on other sites
If you really want to learn about compression algorithms, buy

The Data Compression Book by Mark Nelson (M&T Books)

It also includes code snippets for the most popular compression methods and a BitIO library which does exactly what you request.

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!