Write/read single bit in file.

Jul 10, 2011 at 7:28am
Hello everybody, I'm a little problem,
need to write / read a single bit in a file. My input bits are in an array of booleans.

I've tried a lot of things on google, but none fit my problem, they write all bytes in the file.

I also need some way to read this information in a binary. If I read a char or int and convert to an array of booleans would work?

Thank you all.
Jul 10, 2011 at 8:40am
Use a loop to combine 8 bits into a byte using bit shifts such as:
b0|(b1<<1)|(b2<<2)|...
Jul 10, 2011 at 3:08pm
Great idea ... Why do not think of it before?. But it will not work: (I'm working with high-performance computing, I need a simpler way to do this. But I'll try and give some notice.
Jul 10, 2011 at 3:49pm
Unless you have some fancy architecture that has one bit bytes, you're going to have to work with more than one bit at a time. The C++ programming language defines the smallest amount of data it can work with to be the char, which is by (C++) definition one byte, which depends on your architecture.

Anything that appears to let you read/write/manipulate single bits is just abstracting away the work for you.
Jul 10, 2011 at 4:37pm
C++ also guarantees that a byte will be at least 8 bits, so you could work on an architecture with 1 bit bytes but your program will still use 8 bits per byte. Or, you could work with 12 bit bytes and your program will behave as though they were 12 bits. Hence, the "at least 8 bits".
Last edited on Jul 10, 2011 at 4:37pm
Jul 10, 2011 at 9:10pm
Actually, C++ insists that a char "shall be large enough to store any member of the implementation’s
basic character set"1 - it does not insist on an 8 bit character set.


[1] ISO/IEC 14882:2003(E), Section 3.9.1 Fundamental types
Jul 10, 2011 at 9:31pm
Ahh, ok, maybe this should be updated :)
http://www.parashift.com/c++-faq-lite/intrinsic-types.html#faq-26.4
I only read the above link, I didn't know the exact specifics.
Jul 10, 2011 at 11:01pm
I have sent Marshall an eMail about it; I'll let you know what he thinks :) He's been on the committee and all sorts, so he probably knows what he's talking about and I've overlooked something fundamental...
Last edited on Jul 10, 2011 at 11:05pm
Jul 10, 2011 at 11:19pm
Assuming you are using some temporary space on the compute node for the file, then you can just mmap it. If the file is on NFS, then that's not generally a good idea.

What is the application?

If it's sharing data across nodes then I would consider using MPI instead.
If it's updating an output file, cache the writes in memory and write out the whole file every few minutes.
If it's just reading initial input and writing final output then just work in bytes and use bitwise operators to access the bits as someone suggested above.

If you do anything based on read or write, then you will be working with an array of bytes. There are no functions to access bits.
Jul 11, 2011 at 9:38am
For completion, I have found one way that the C++ standard insists on an 8 bit byte minimum, but it did require going into the C standard that C++ agrees to (mostly) abide by and start digging around in <climits> and the like.

Anyway, I stand corrected. There is a guarantee that a byte will have at least 8 bits, although the implications that prove it are spread across an awful lot of paper :)
Topic archived. No new replies allowed.