Thread: reading/writeing sequence of bits (not bytes) from/to file

  1. #1
    Registered User
    Join Date
    Jul 2002

    reading/writeing sequence of bits (not bytes) from/to file

    Hi everyone,

    I am doing some low end stuff in Linux or Unix environment, just wondering how can I read a sequence of bit from file to memory, and make some change on it and then write it back to the file?

    also I have tried

    ofstream out(somefile.c_str());
    struct Bits {
    unsigned char * b:6; // create a bit field with
    // 6 bits
    } theBit;

    for (int i = 0; i < 20; i++) {
    theBit.b = 'b';
    out << theBit.b;

    and the file size is 20 bytes instead of 20x6 bits (15 bytes)

    anyone know why?

    thanks first...

  2. #2
    Registered User
    Join Date
    Jul 2002
    Because characters are 8 bits not 6....?

    On top of that your using a pointer and not a varible..
    Last edited by Jaggedyer; 07-04-2002 at 11:08 AM.

  3. #3
    Registered User raimo's Avatar
    Join Date
    Jun 2002
    Write the 6-bit long variables to some buffer and write the buffer to disk. ofstream::operator<<() is maybe unable to write non-byte sized variables.

  4. #4
    thanks for replying,

    I have tried to make it to a variable instead of a pointer, and tried the out.write method instead of the << operator, but it only accept
    ostream::write( char *, int) or other pointer to signed or unsigned char, and I cannot make a pointer pointing to a bit field.

    so how can I create the buffer and how to write the buffer to disk?


  5. #5
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002


    The smallest amount of data you are going to be able to write is a byte. You are not going to be able to write single bits on their own.

    However, you could:

    Use each byte to represent a bit, and write those. This is standard, you should notice that the bool data type will actually have a size (use sizeof) of 8 bits, rather than one bit. This is the simplest way of doing it and is fine if you are not too concerned about efficiency.

    Alternatively, if you really only want to use 1 bit to store 1 bit of information, you need to pack the bits into a buffer, where the shortest length of the buffer is going to be 1 byte.

    You can do this using the bitwise operators of C++. I.e., to store the sequence 101 in a byte explicitly, do the following:

    int byteBuf = 0;
    byteBuf |= 0x01;
    byteBuf |= 0x04;

    Here we set the corresponding low order bits to on by using the OR (|=) operator.

    To explicitly check for bit states, you can

    if ( byteBuf & 0x01 )


    if ( byteBuf & 0x04 )

    From lowest, to highest, bit values in are specified in hex as follows:

    first : 0x01 (correspond to 00000001)
    2nd : 0x02 (00000010)
    3rd : 0x04 (00000100)
    4th : 0x08 (00001000)
    5th : 0x10 (00010000)
    6th : 0x20 (00100000)
    7th : 0x40 (01000000)
    8th : 0x80 (10000000)

    OS: Windows XP
    Compilers: MinGW (Code::Blocks), BCB 5

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-23-2007, 01:48 PM
  2. Basic text file encoder
    By Abda92 in forum C Programming
    Replies: 15
    Last Post: 05-22-2007, 01:19 PM
  3. strcat - cannot convert char to const char
    By ulillillia in forum C Programming
    Replies: 14
    Last Post: 12-07-2006, 10:00 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 10:36 AM