arraysfilesavebooleanstream-compaction

How to store large arrays of booleans in file with the most light size?


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


Solution

  • 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:

    uint8 bitFlags = 0x00; // empty mask  0000 0000
    
    uint8 maskA = 0x01;    // first bit:  0000 0001
    uint8 maskB = 0x02;    // second bit: 0000 0010
    

    To set a variable, use bitwise OR (preserves all other bits but sets the bit you're interested in:

    bitFlags = bitFlags | maskA;
    

    Read a variable:

    bool isTrueA = ( bitFlags & maskA ) > 0 ; // bitwise AND, result is 1 if bit is set, 
                                              // 0 otherwise
    

    Reset a variable:

    bitFlags = bitFlags & (!maskA);           // bitwise AND with inverse mask: 1111 1110
                                              // forces position of A to be zero
    

    More information on the use of bitmasks can be found here.