|
Re: Best way to check multiple Boolean values?
Just to add a bit more to the excellent suggestions. If you have say six buttons, it is sometimes useful to see how many are on, or to see if one of the first two, one of the second two, and one of the last two are on.
For searching how many are on, another trick is to send the Boolean array to the Boolean to (0,1) conversion function. This will give you an array of integers with a 0 for falses and a 1 for trues. Then you can do things like Sum in the numeric palette to answer how many Booleans were true in the array.
To check for combinations, you can use Boolean operations directly on arrays, and sometimes that is nice. For example to And your buttons with a mask to see if specific ones are on. If you fine the arrays of Booleans difficult to read off, some people prefer to cast them to numbers and then do Boolean operations on the integers, for example MyInt AND Mask. In other words, the Boolean operations work on integers as well as Booleans.
Finally, to make the most self documenting code, it is often better to build clusters of the Booleans, or to convert the array to a cluster. The cluster allows you to name the individual Booleans, and of course you can use Boolean operations on the clusters too. So MyBooleanCluster AND Mask gives a very readable bit of code. It is often then useful afterwards to do quick conversions to integers to compare with 0 or to convert to array to search, etc. If you want to spend the time to make a typedef cluster of your Boolean cluster, you can make very readable code and cut out lots of logic debugging.
Greg McKaskle
|