My program produces large arrays filled with booleans. I need the most compact way to save those in file.
I read here http://www.kirupa.com/forum/showthread.php?339670-How-is-boolean-represented-in-memory that 8 booleans in memory may represented as single byte, so single boolean is single bit. But how I can save those arrays of booleans same compact into a file? I know - file write functions is kind of operate with bytes, instead of bits.
Shortly - I want to be able to save arrays of booleans into file, with size 8 times less than Array.Length
I presume you are coding in c++, since the link you posted leads to a c++ forum?
You can use bitmasks and bit operations, where each boolean is located at a certain bit of your byte. Therefore, you can use one byte (uint8) to store 8 booleans. This is quite low level and I used to do this when coding in C and where storage was an big issue.
If you really want to go down that road, you best hide the operations below in some higher level read/write and set/reset functions. After you transform your booleans into bytes (you have an array of bytes now!), you can simply write them to binary files as described here.
Here's some hints on how booleans can be converted to bytes: You create masks for each boolean you're interested in to specify at what location in the byte they are going to be written / read.
I'm using hexadecimal numbers for better readability.
For example:
To set a variable, use bitwise OR (preserves all other bits but sets the bit you're interested in:
Read a variable:
Reset a variable:
More information on the use of bitmasks can be found here.