C#, using the FlagsAttribute on enumerations

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rajanipro
    New Member
    • Jul 2007
    • 11

    #1

    C#, using the FlagsAttribute on enumerations

    Please explain to me the following C# program, which I came across in MSDN help:

    // enumFlags.cs
    // Using the FlagsAttribute on enumerations.
    using System;

    [Flags]
    public enum CarOptions
    {
    SunRoof = 0x01,
    Spoiler = 0x02,
    FogLights = 0x04,
    TintedWindows = 0x08,
    }

    class FlagTest
    {
    static void Main()
    {
    CarOptions options = CarOptions.SunR oof | CarOptions.FogL ights;
    Console.WriteLi ne(options);
    Console.WriteLi ne((int)options );
    }
    }


    Output:
    SunRoof, FogLights
    5


    Comments:
    Notice that if you remove FlagsAttribute, the example will output the following:
    5
    5
  • TRScheel
    Recognized Expert Contributor
    • Apr 2007
    • 638

    #2
    If you have each value follow a single bit pattern you can use multiple enum values (hence, flags) in a single enum. This is useful when a flag represents multiple things. I see it commonly used in DirectX.

    To get this result, declare you enum with the values being incrementally larger versions of 2^n. A sample flagable enum would start at 1, and continue from there, as follows:

    1
    2
    4
    8
    16
    32
    64
    128
    256
    512
    1024
    2048
    4096
    ... etc

    Using bytes, this is shown as:

    0000 0001 = 1
    0000 0010 = 2
    0000 0100 = 4
    0000 1000 = 8
    0001 0000 = 16
    0010 0000 = 32
    0100 0000 = 64
    1000 0000 = 128

    How this works:

    If you flag any number of enum objects following that pattern, you can easily discern what value they correspond to you. Take for instance:

    0000 0001
    0001 0000

    If you flagged those with the OR symbol, the resulting enum value would be:

    0001 0001

    Now there are only two possible values that would create that byte. This can be used to any size too. For instance:

    1001 0101

    Only grouping four of those unique bytes will result in this byte.

    If it is still confusing, I will explain more.

    Comment

    • TRScheel
      Recognized Expert Contributor
      • Apr 2007
      • 638

      #3
      Some simple bitwise operations include:

      The AND operation, only returns 1 if both are true
      Code:
      1 & 0 = 0
      0 & 1 = 0
      1 & 1 = 1
      0 & 0 = 0
      The OR operation, returns 1 if either are true
      Code:
      1 | 0 = 1
      0 | 1 = 1
      1 | 1 = 1
      0 | 0 = 0
      The NOT operation returns 1 if the bit is 0
      Code:
      ! 1 = 0
      ! 0 = 1
      The XOR operation returns 1 if the bits are different
      Code:
      1 ^ 0 = 1
      0 ^ 1 = 1
      1 ^ 1 = 0
      0 ^ 0 = 0

      Comment

      • rajanipro
        New Member
        • Jul 2007
        • 11

        #4
        Cool! I got it.

        But why is the output:
        5
        5


        different when compiled without the FlagsAttribute [Flags]?

        Comment

        • TRScheel
          Recognized Expert Contributor
          • Apr 2007
          • 638

          #5
          Ah that flag. Well it informs the framework that your enum is usable as flags, and thus can do more advanced work on it.

          I suggest looking at this site:

          MSDN - FlagsAttribute

          Comment

          • rajanipro
            New Member
            • Jul 2007
            • 11

            #6
            Thank you very much indeed!

            Comment

            Working...