Use enum value to mark BitArrays : Bits « Data Type « C# / CSharp Tutorial






using System;
using System.Collections;

    enum MyEnum
    {
        A, B, C, D
    }

    class Coffee
    {
        private BitArray ingredientFlags = new BitArray(8);

        public void AddIngredient(MyEnum type)
        {
            ingredientFlags[(int) type] = true;
        }

        public void ListMyEnum()
        {
            for( int i = 0; i < ingredientFlags.Count; i++ )
            {
                if( ingredientFlags[i] )
                    Console.WriteLine( "-" + Enum.GetName(typeof(MyEnum), i) );
            }
        }

        public void Drink()
        {            
            ingredientFlags.Xor( ingredientFlags );
            ListMyEnum();
        }
    }

  class Class1
  {
    [STAThread]
    static void Main(string[] args)
    {
            Coffee java = new Coffee();

            java.AddIngredient( MyEnum.B );
            java.AddIngredient( MyEnum.C );
            java.AddIngredient( MyEnum.A );

            java.ListMyEnum();

            java.Drink();
    }
  }








2.42.Bits
2.42.1.Bitwise Operators
2.42.2.Display the bits within a byte.
2.42.3.Use enum value to mark BitArrays
2.42.4.Converts the bit patterns of UInt32 values to Byte arrays with the GetBytes method.