Let’s say we have a list of four things. Here’s a list I’ve copied and pasted from shethinkstoherself blog --
1) monsoon rains and the smell of earth it gives rise to
2) skype for the long distance intimacy it allows which is missing in e-mails/facebook and other mediums of communication
3) silence that you can be comfortable in and with which allows for depth in serenity
4) skin – all the colors and variation in shades, a rainbow of races and our differences
Let’s say the order of these things doesn’t matter but each possible combination is important. After some computation we can arrive at a conclusion that there will be 16 such possible combinations if we also include the option of doing/experiencing nothing in this list as sad as that may be. And obviously the number of such possible combinations will grow really fast, shall we say at an exponential rate, as the number of things in this list grows.
The challenge here for a programmer is how to manage situations where we want to provide an option to do an exactly one possible combination. If we want to use a “switch” statement and enumerate all possible combinations, it won’t be quite manageable as the size of the list goes to, say, just 10. There will be 1024 possible combinations.
switch(option)
{
case 1:
choose combination number 1;
break;
case 2:
choose combination number 2;
break;
…
case 1024:
choose combination number 1024;
break;
};
will be quite long and will involve a lot of thoughtless and error prone typing if I decide to actually type everything out instead of using the ever so handy “…” .
However, I found out today trying to deal with a similar situation at work that a simple bitwise AND logical operator (&) can make things quite simpler. First thing’s first. AND operator returns true if both the operands are true. There are other logical operators like ‘or’ and ‘xor.’ ‘or’ returns true if any or both the operands are true while ‘xor’ returns true only when exactly one of the operands is true and returns false if both the operands are true or if both the operands are false. Example – 100 and 101 = 100, 100 or 101 = 101, 100 xor 101 = 001 while doing bitwise operation.
Now let’s put this ‘and’ operator to its magical use on our problem of choosing a combination of things from our list of four things where there are 16 possible combinations. Let’s label four things in the list as 1, 2, 4, and 8. Now we can label various 16 combinations 0 through 15. Let’s say this incoming variable is OPTION. If we use AND operation between 1, 2, 4, 8 and the incoming OPTION we won’t have to enumerate all options like we did in our switch statement above. Keep in mind various programming languages have separate operators for regular AND logic and bitwise AND operation. Assuming & is the bitwise AND operator, we can do something like the following –
IF (OPTION & 1 > 0){
do list number 1;
}
IF (OPTION & 2 > 0){
do list number 2;
}
IF (OPTION & 4 > 0){
do list number 3;
}
IF (OPTION & 8 > 0){
do list number 4;
}
If we pick 3, then it includes a combination of 1 and 2 and passes the first and second 'if' checks but not the latter two. For example 3 in binary is 11 and 1,2,4, and 8 are 1, 10, 100, and 1000. 3 & 1 = 11 & 01 = 01 which is greater than 0. 3 & 2 = 11 & 10 = 10 > 0. But, 3 & 4 = 011 & 100 = 000 which is NOT greater than 0 and 3 & 8 = 0011 & 1000 = 0000 which is also NOT greater than 0.
If we pick 3, then it includes a combination of 1 and 2 and passes the first and second 'if' checks but not the latter two. For example 3 in binary is 11 and 1,2,4, and 8 are 1, 10, 100, and 1000. 3 & 1 = 11 & 01 = 01 which is greater than 0. 3 & 2 = 11 & 10 = 10 > 0. But, 3 & 4 = 011 & 100 = 000 which is NOT greater than 0 and 3 & 8 = 0011 & 1000 = 0000 which is also NOT greater than 0.
Doing this, we can cover 1024 possible combinations in mere 10 ‘IF’ checks. I thought it was pretty cool, so I thought I’d share, even though a lot of you already knew this trick.