Bit-by-bit Reading from Memory

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kenone
    New Member
    • Jul 2008
    • 3

    Bit-by-bit Reading from Memory

    How do you read a binary file in memory bit-by-bit?

    ifstream file ("my.bin", ios::in|ios::bi nary|ios::ate);
    if (file.is_open() )
    {
    size = file.tellg();
    memblock = new char [size];
    file.seekg (0, ios::beg);
    file.read (memblock, size);


    I was using file.get() but returns the next char I really want the next bit.

    Any help is appreciated. :-)
  • Savage
    Recognized Expert Top Contributor
    • Feb 2007
    • 1759

    #2
    You can only get a byte(char) when reading from the file.If the next bit is what you need you can pull it out from the returned byte using bitwise operators. Anyway why do you need to pull out next bit from the text file?

    Comment

    • kenone
      New Member
      • Jul 2008
      • 3

      #3
      Originally posted by Savage
      You can only get a byte(char) when reading from the file.If the next bit is what you need you can pull it out from the returned byte using bitwise operators. Anyway why do you need to pull out next bit from the text file?
      I read in a binary file which is about 1/2 Gb to a char array. I then have to search for the first occurrance of 10101 or 111, that is the header of a message, what follows is the data of different size fields. I have found it too difficult, and cumbersome, to search for the pattern (using file.get())and then extract the fields as the pattern can start in any bit in a byte (for instance the lsb could be the start of the pattern 10101 but for the next message it could start at the 4th bit). Ideally it would be great to copy the char array to a vector<bool> then interrogating the vector would be easy, but I don't know how to do this. One message is 83 bits long and the other message is 41 bits long.

      Any help would be much appreciated as I have been trying to solve this for about two weeks now and it is driving me nuts. :-)

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        Hi,
        Go through the file char by char, looping through the bits in the char and push_back()ing them into your vector<bool>. To get a particular bit out of the char, you:
        create a char with a true bit in only that place, eg 00000010 for place 1, or 00100000 for place 5, and
        use the bitwise & operator to compare that to your original char, eg
        00100000 & 01001010
        The result will not have any true bits in it, unless both chars have a bit in that place, and if the answer has any true bits in it, it will be true when you cast it to bool.
        To get a char with only a bit in a particular place, you do:
        1 << place
        to slide 1 into the right place. You will actually get an int, but it will work fine.
        So:
        Code:
        for (int place = 0; place < 8; place++)
            myVector.push_back(myChar & (1 << place));
        Hope this helps.

        Comment

        Working...