Thread: reading and writing a single bit to a file

  1. #1
    Registered User
    Join Date
    Sep 2005
    Posts
    2

    is reading and writing a single bit to a file possible?

    is there any way to read/write a single bit from/to a file in c ?

    I'm trying to creat a simulation (i'm a physicist not a programmer...) and the data is boolean (meaning yes or no) and the natural way to store it is in bits (dah..).
    since there's TONS of data the files will be HUGE if i'd use integers. so i need to know how to handle single bits.
    please help...
    Last edited by kharn; 09-04-2005 at 07:30 PM.

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Read a char and then use bit masking to get at a single bit.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Registered User
    Join Date
    Sep 2005
    Posts
    2
    pardon me , but I have no idea what "bit masking means". . .
    p.s - do you mean reading a single character using fread or getc?

  4. #4
    aoeuhtns
    Join Date
    Jul 2005
    Posts
    581
    You can only read a character at a time, not a bit at a time. So you need to read a character, and then look at its particular bits, and modify one of them, and then write out the character.

    Suppose your piece of data is in the fourth-lowest bit in the character. Your character might be filled with some data. Suppose its eight bits happen to represent the number 98:

    Code:
    01100010
       ^   $
    I put the arrow to point at the fourth-lowest bit in the character. (Realize that I like to start at zero. The dollar sign is beneath the zero-lowest bit in the character.)

    Now let's look at the number 16:
    Code:
    00010000
       ^
    If we bitwise-or those numbers together, we get:
    Code:
    01100010 <- operand
    00010000 <- operand
    01110010 <- result
    We've changed the fourth bit of the number!

    In general, following the spirit of the above example, you can use the bitwise operations &, |, ^, and ~ to change the bits of a number. Here are some sample functions.

    Code:
    unsigned int turn_on_but(unsigned int n, int bitnum) {
        return n | (1 << bitnum);
    }
    
    unsigned int turn_off_bit(unsigned int n, int bitnum) {
        return n & (~ (1 << bitnum));
    }
    
    unsigned int flip_bit(unsigned int n, int bitnum) {
        return n ^ (1 << bitnum);
    }
    
    /* Returns 1 or 0 */
    unsigned int get_bit(unsigned int n, int bitnum) {
        return (n & (1 << bitnum)) >> bitnum;
    }
    
    /* Expects 1 or 0 */
    unsigned int put_bit(unsigned int n, int bitnum, int bit) {
        return n | (bit << bitnum);
    }
    So you could read a character, call one of these functions (getting a specific bit from the character), use these functions to write a bit to the right place in the character, and write the new character, if you wanted. You can only read or write a character at a time, though.

    This is (not exclusively) what bit masking is. For example, the number 16 (00010000 in binary) is a 'mask' that only lets the fourth bit shine through when 'bitwise anded' with a number.
    Last edited by Rashakil Fol; 09-04-2005 at 08:27 PM.

  5. #5
    Registered User
    Join Date
    Sep 2004
    Posts
    44
    Bit Manipulation

    You could also use 'Bit Fields', do a search.
    Last edited by n7yap; 09-04-2005 at 10:50 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. segmetation fault (reading and writing file in c)
    By tasosa in forum C Programming
    Replies: 5
    Last Post: 04-13-2009, 06:04 AM
  2. Reading out of and writing into the same file
    By Wiretron in forum C Programming
    Replies: 8
    Last Post: 12-30-2006, 02:04 PM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. file writing and reading
    By Micko in forum C Programming
    Replies: 8
    Last Post: 01-13-2004, 11:18 AM
  5. what does this mean to you?
    By pkananen in forum C++ Programming
    Replies: 8
    Last Post: 02-04-2002, 03:58 PM