What does | operator stand for Enum

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • cmrhema
    Contributor
    • Jan 2007
    • 375

    #1

    What does | operator stand for Enum

    Hi,
    I have the following example
    Code:
    enum MaterialColors
        {
            Blue=1,Red=2,Yellow=4, Purple=Blue|Red,Green=Yellow|Blue,Orange=Red|Yellow,
        }
        class Program
        {
            static void Main(string[] args)
            {
                int j = (int)MaterialColors.Orange;
                Console.WriteLine(j.ToString());
                Console.Read();
            }
        }
    Here what does the operator "|" stand for.
    The answer is 6, but what does the "|" operator actually do.
  • vekipeki
    Recognized Expert New Member
    • Nov 2007
    • 229

    #2
    It's a bitwise OR operator: http://msdn.microsoft.com/en-us/libr...kx(VS.80).aspx

    Check this also: http://en.wikipedia.org/wiki/Bitwise_operation.

    Comment

    • cmrhema
      Contributor
      • Jan 2007
      • 375

      #3
      Oh so u mean its similar to "<<" and ">>" operator.
      shift left wise and rightwise

      many thanks vekipeki

      Comment

      • cmrhema
        Contributor
        • Jan 2007
        • 375

        #4
        But if I replace it with & operator, I get the answer as 0, I dont think its logical operator then.

        Comment

        • vekipeki
          Recognized Expert New Member
          • Nov 2007
          • 229

          #5
          Be sure to read this: http://en.wikipedia.org/wiki/Bitwise_operation, and compare bitwise OR to bitwise AND. Try writing your numbers (1, 2 and 4) in binary form (0001, 0010 and 0100), and then compare with the examples the article above.

          Note that there is a difference between logical and bitwise operators: http://msdn.microsoft.com/en-us/libr...5d(VS.80).aspx.

          Comment

          • cmrhema
            Contributor
            • Jan 2007
            • 375

            #6
            thanks vekipeki. Should have replied long back. Thanks for reminding me the basics.

            Comment

            Working...