Best way to read one bit at a time from a binary file?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • goldmandrummer
    New Member
    • Nov 2013
    • 1

    Best way to read one bit at a time from a binary file?

    I'm trying to do file compression/decompression in C and I need to handle one bit at a time. I've looked around online and haven't seem to found anything that I can figure out how to implement nicely.

    I currently try to do:

    unsigned char byte = fgetc(fptr);

    and later

    byte >>= 1;

    but the problem is that I have to use the first bit of the byte and then treat the next 8 bits as one byte. The byte usage keeps shifting over in this way. It's probably quite clear that I'm a bit lost. Any help would be greatly appreciated!
    Last edited by goldmandrummer; Nov 9 '13, 08:42 AM. Reason: Forgot to mention language.
  • weaknessforcats
    Recognized Expert Expert
    • Mar 2007
    • 9214

    #2
    C you not just do a bitwise AND of the first bit using an appropriate maskk determine the state of the first bit?

    I don't know what you means by "first bit". Is that the first bit read in from the file. Or is it the last bit of the first byte read in from the file? Is this Big Endian or Little Endian?

    Comment

    Working...