For example, I have 3 counters (which I essentially want to represent as truth values (counter > 0 true; false otherwise). This leads to 2^3 = 8 permutations for my truth values as shown:
000
001
010
011
100
101
110
111
Then each permutation maps to a state. How do I convert these counters to a binary representation at the bit level, and then how would I use the binary representation in a switch structure to map to a state? (ex. 001 maps to “contains x” and 010 maps to “contains y”, and 011 would map to “contains x and y”. Will this all be portable to other operating systems as well?
One possibility is to define constants with the value for each bit corresponding to each counter:
Then based on the counter values (zero or non-zero) set the bits. The
|operator performs a bitwise logical OR operation (see this for more information). Ifctr1is nonzero, it just sets the lowest order bit in the variablestate. Ifctr2is nonzero, it sets the second bit instateto a 1 and leaves other bits unchanged. etc.Then switch on the possible state values: