Let me start with some background:
By "tribool" I understand a variable which can hold one of the following values: true, false or null.
In question Copying array of ints vs pointers to bools , the OP wanted to have an array of tribools (more or less) which would be as small as possible.
With "a bit of" most basic bit-fu I came up a solution which used 2 bits per tribool and allowed to store the OP's array of 64 tribools in 16 bytes, which is OK.
The tribool mechanics I used were simple, like:
- boolean A means "null or not null",
- boolean B means "true or false if not null".
But then I thought... An algorithmical definition of a "bit" is:
A bit is the amount of information which specifies which of two equally probable events shall occur.
Clearly a true/false value is 1 bit big. Two true-false values as a whole are 2 bit big.
And what about our conceptual tribool?
My point is: In terms of the size of contained information, a tribool is bigger than 1 bit but smaller than 2 bits.
- Justification 1: Assume we implement our if boolean as described above. If boolean A is "null", the value of boolean B is redundant and doesn't carry any relevant information.
- Justification 2: It's impossible to store information from 2 independent boolean values in one tribool, so it has
(None of the above is a formal proof, but I believe that we can agree that about the "size" of the tribool being strictly bigger than 1 bit and strictly smaller than 2.)
My question is:
How to programatically take advantage of the fact that a tribool has less information than 2 bits, and implement in software (c, c++?) an array of N tribools which would have the memory footprint smaller than N/4 bytes for some N?
Yes, I do understand that such an implementation isn't really hardware-friendly and would perform slower than any common solution with redundance (as those presented in the OP's question). Let's just optimize for space, not for efficiency.
Clearly this implementation needs a different representation of a tribool than a pair of bools (which is by itself redundant, as described before). The theory says it's possible to achieve that goal and I like to see an actual implementation. Any ideas?