I wonder why you go to logic discussion????
& and | are considered to be logical operators when we have bool values,I mean 0 and 1 (false and true) or when we deal with conditions and comparisons that return true or false.
But here the first and third lines say that myColor is of type int and if | or & is applied to myColor,it is known as a number (like 0,1,5,100,...) to these operators.look at this part:
Code:
myColor = myColor | 2;
| doesn't seem to be a logical operator.Does it?
Because the value 2 is out of the range of a bool value so | is a bitwise operator here.
Till here,because we didn't have bool value, | was treated as bitwise operator.
but in action (during the computation of 0000 | 0010) it is a logical operator,because it's operands are 0 and 1.Look at this:
Code:
myColor = myColor | 2;// (===> 0000 | 0010 = 0010 = 2)
As you see | is logical operator here BUT it is none of our business.It is related to ALU part of CPU.Just get out of logic here.
Line 4 is analyzed as line 3 regardless of any logical operator but bitwise operator.After the execution of this line,myColor is assigned the value 6 (0010 | 0100 = 0110 = 6)
About the last line:
Code:
containsRed = (myColor & 4) == 4;
Again inside the parenthesis,& is a bitwise operator and after evaluation we have the value 4 inside parenthesis (0110 & 0100 = 0100 = 4).
The rest of the code is the comparison 4==4,which assigns the value true (or 1) to containsRed which is a variable of type bool.
Hope you got the point...
Erfan