Search
Thursday, May 17, 2012 ..:: Home ::.. Register  Login
   Calendar  
     
  
   Search  
     
  
   Blogroll  
     
  
   Disclosure  
All blog entries are the opinions of the author and do not necessarily reflect the opinions of their employer. All the code presented is for explanation and demonstration purposes only. Any damages incurred to your site and/or data are not the responsibility of the author. Every effort is taken to ensure the code properly compiles, however sometimes there are some hiccups and you might be required to do your own debugging.
     
  
   TechTidBits (Blog)  

Can you tell me what is wrong with this compile error?

Oct 9

Written by:
Friday, October 09, 2009 10:23 PM  RssIcon

Today SB and I were talking about how we used to do "Coding Challenges" at work with SG and how we missed them.  They were just quick little code questions to each other to try to stump the other, but nothing wickedly complicated or too long to diagnose.  In that tradition, I submit a doozie for your contemplation.  In other words........ H E L P !!!!!!!!! :> LOL

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).

Using Flags but getting compile error

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!

Compile error

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!

 

Tags:
Categories:
Location: Blogs Parent Separator TechTidBits

Your name:
Gravatar Preview
Your email:
(Optional) Email used only to show Gravatar.
Your website:
Title:
Comment:
Add Comment   Cancel 
     
  
Copyright 1999-2012 by PCHenry.com   Terms Of Use  Privacy Statement