Thread: Bit by Bit!!!

  1. #1
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683

    Bit by Bit!!!

    Is there a way i can ready bit by bit from a file and write it back in the same way..(010101011101010101)...


    Thanx for any help....
    vasanth

  2. #2
    I lurk
    Join Date
    Aug 2002
    Posts
    1,361
    Read it in 1 char at a time and manipulate its 8 bits?

  3. #3
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    Go with Eibro's solution. There is no point trying to do otherwise since the disk subsystem will be reading blocks of data into it's cache any way.
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  4. #4
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    ok thanx...
    So writing back i will have to put 8 bits back into a char and write it into a file..

    Well i will be working on binary files... do all binary files have the block size fixed.. How do you handel if the end of the file has just got 1 or 2 bits remaining to be read or written back.. i cannot write the entire char back.. or it it that this never happens... is it always in multiples of 8 bits .....

    if you are wondering why.. for the compression contest planin to handle files in terms of bits to achive greater compression..

  5. #5
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    and one more doubt.. since there is not data type called bit... how do i manipulate the bits in my program... I have to build a tree where a node contains 3 or 4 bits... So do i use int to hold each bit 0 and 1.. and while writing it back to file i can construct a byte taking 8 ints and store it.. is this recomended or is there better ways.. and what is the size of a boolean.. it is i bit or does it use int to represent true or false....

  6. #6
    mustang benny bennyandthejets's Avatar
    Join Date
    Jul 2002
    Posts
    1,401
    I think the question is, Salem, is anyone able to answer all of vasanth's questions? There are about 300 questions per post. Not that that's a bad thing of course.
    [email protected]
    Microsoft Visual Studio .NET 2003 Enterprise Architect
    Windows XP Pro

    Code Tags
    Programming FAQ
    Tutorials

  7. #7
    Its not rocket science vasanth's Avatar
    Join Date
    Jan 2002
    Posts
    1,683
    well come on guys.. i am not asking you to code the program for me.. or not aksing you how to compress.. it is like any other programming doubts... and it is just a part of it..


    thanx in advance(hopefully)
    vasanth

  8. #8
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    A couple of points...

    Because of the parallel-data hardware design of computers, you can't address/access an individual bit. In a 32 bit system, any read/write to memory will involve 32-bits. You have to use bit masking if you want to read or manipulate an individual bit. If you are writing characters to the disk, the OS and BIOS will "pack" 4 characters into 32 bits.

    You can use sizeof(bool) and you'll probably find that it is the same as an int. It's just a programming convenience.

    The "binary" file type is sort-of misnamed... All files are stored in binary format, and all memory is binary. A binary file has no particular format (as far as the OS is concerned). Any byte in a binary file can contain any value.

    So, like Eibro suggested - The input-output for you program should be chars. You'll read in characters, do some magic to squeeze the bits into fewer characters, then write the new characters to the disk.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 11-10-2005, 10:53 AM
  2. porting application from 32 bit to 64 bit error
    By gandalf_bar in forum Linux Programming
    Replies: 1
    Last Post: 09-14-2005, 09:20 AM
  3. Bit processing in C
    By eliomancini in forum C Programming
    Replies: 8
    Last Post: 06-07-2005, 10:54 AM
  4. Bit Manipulation Questions
    By CPPNewbie in forum C++ Programming
    Replies: 7
    Last Post: 08-12-2003, 02:17 PM
  5. Array of boolean
    By DMaxJ in forum C++ Programming
    Replies: 11
    Last Post: 10-25-2001, 11:45 PM