Order of operations/precedence in programming languages are not to be taken lightly. Every bit head worth their weight in C++ manuals knows that * is higher up the order of precedence than +. But what about the && and & operators? And what about the +=? Do you know how they fit in? hhhmm still confused a bit? Every try a piece of math for your grade 12 calculus and get it wrong, then put brackets around something and BOOM it works? THAT'S "order of operations" at work.
Today I was playing with some enumerations which I wanted to use as Flags. No big deal right? You've probably seen this type of things (or some reasonably similar facsimile).
[Flags]
public enum NhlTeams
{
Habs,
Sens,
Leafs,
Sabres,
Bruins
}
But my question/source of confusion comes from USING these flags in an if statement.
hhhmm let me pause for a moment and let you think about how would YOU use these? If you're thinking of the && operator, you're close, VERY close. Actually use just one & and | and you're nearly bang on. Why "nearly?" Well, let me show you what happened when I first typed this out (in my defence I was working on my first coffee of the day at this time).

Don't try to cut'n'paste the code, it's a picture. Why? Cause I wanted to show you the blue squiggles underneath the if condition. WTF is going on with the "order of operations" here? Why is the bit-wise AND operator trying to have higher precedence over the equality operator?
You want proof this is a compile error? Sure!

BUUUUUUUUT if I add brackets around the condition (just the condition) it all compiles and runs juuuuuuuuuuust fine! WTF?
static void Main( string[] args )
{
NhlTeams myFavourTeams = NhlTeams.Bruins | NhlTeams.Habs;
if( (myFavourTeams & NhlTeams.Habs) == NhlTeams.Habs )
{
Console.WriteLine( "GO HABS GO!" );
}
if( (myFavourTeams & NhlTeams.Leafs) == NhlTeams.Leafs )
{
Console.WriteLine( "Are you crazy?!" );
}
Console.ReadLine();
}
Ok, back to "the why?" According to MSDN's own pages, Precedence and order of Evaluation, the == is higher than the & operator. So my question is WHY the compile error? Oh before you start peppering me for what's the right answer? I don't know, I would like to start a dialog to find out the true why!