CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Bitwise 'OR' with enumeration

    I cant believe Ive never covered this before, but sometimes I see functions which take enumerations as arguments such as:

    Code:
    FileStream fStream = new FileStream("myfile.txt", FileMode.OpenOrCreate, FileAccess.Read | FileAccess.Write);
    And I was just curious as to how that enumeration is implemented inside a particular function. Does it use a switch - case block? Or if-else if-else block? Im creating a new exception handling framework and was curious as to how I could implement the bitwise OR operator with my own enumerations. Like so:

    Code:
    // ExceptionPolicy is a custom static class which handles exceptions
    // Handle will handle the exception in the way specified by the ExceptionMedium enumeration
    // ExceptionMedium.EventLog tells the Handle method to write the exception data to the windows event log
    // ExceptionMedium.Report opens a report dialog allowing the user to report the exception to our 
    // internal databases
    ExceptionPolicy.Handle(myException, ExceptionMedium.EventLog | ExceptionMedium.Report);
    Last edited by RaleTheBlade; August 5th, 2009 at 05:37 PM.
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  2. #2
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Bitwise 'OR' with enumeration

    It all starts with setting the [Flags] attribute on the enum.

    http://msdn.microsoft.com/en-us/libr...attribute.aspx

  3. #3
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Bitwise 'OR' with enumeration

    They likely AND the supplied value with a single enum value to see if it exists. For example:

    Code:
    enum Foo
    {
      A = 1,
      B = 2,
      C = 4,
      D = 8
    }
    
    // ...
    
    void SomeFunc( Foo arg )
    {
        if ( (arg & Foo.A ) == Foo.A )  // you could also check for > 0
        {
            // 'arg' contains Foo.A
        }
    }
    You don't actually need the Flags attribute.
    Last edited by BigEd781; August 5th, 2009 at 06:11 PM.

  4. #4
    Join Date
    Jul 2007
    Location
    Illinois
    Posts
    517

    Re: Bitwise 'OR' with enumeration

    Awesome, I knew there had to be some trick... if I only knew my bitwise operators a bit better but alas I dont use them very often.

    Thanks
    R.I.P. 3.5" Floppy Drives
    "I know not with what weapons World War III will be fought, but World War IV will be fought with sticks and stones." - Albert Einstein

  5. #5
    Join Date
    Jun 2008
    Posts
    2,477

    Re: Bitwise 'OR' with enumeration

    Quote Originally Posted by RaleTheBlade View Post
    Awesome, I knew there had to be some trick... if I only knew my bitwise operators a bit better but alas I dont use them very often.

    Thanks
    Yeah, you can code many applications in C# without manipulating bits, but any programmer really should know this stuff as it eventually crops up. I do a fair amount of image processing in my work so it comes up often.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured