title: Decoding a bitmask from a value in C#
body: <p>I am trying to decode a bitmask</p>
  
  <pre><code>[Flags]
  public enum Amenities
  {
      BusinessCenter = 1,
      FitnessCenter = 2,
      HotTub = 4,
      InternetAccess = 8,
      KidsActivities = 16,
      Kitchen = 32,
      PetsAllowed = 64,
      Pool = 128,
      Restaurant = 256,
      Spa = 512,
      Whirlpool = 1024,
      Breakfast = 2048,
      Babysitting = 4096,
      Jacuzzi = 8192,
      Parking = 16384,
      RoomService = 32768,
      AccessibleTravel = 65536,
      AccessibleBathroom = 131072,
      RollShower = 262144,
      HandicappedParking = 524288,
      InRoomAccessibility = 1048576,
      AccessibilityDeaf = 2097152,
      BrailleSignage = 4194304,
      FreeAirportShuttle = 8388608,
      IndoorPool =  16777216,
      OutdoorPool = 33554432,
      ExtendedParking = 67108864,
      FreeParking = 134217728
  }
  </code></pre>
  
  <p>How do I write a function that decodes a value like 5722635 and returns a list of all Amenities that are encoded in 5722635.</p>
  
  <p>the result should look like this:</p>
  
  <p>This Property has the following Amenities:</p>
  
  <ul>
  <li>Business Center</li>
  <li>Fitness Center</li>
  <li>Internet Access</li>
  <li>Available Spa On-site</li>
  <li>Babysitting</li>
  <li>Parking</li>
  <li>Accessible Path of Travel</li>
  <li>Accessible Bathroom</li>
  <li>Roll-in Shower</li>
  <li>In-room Accessibility</li>
  <li>Braille or Raised Signage</li>
  </ul>
  
  <p>I have been trying things like</p>
  
  <pre><code>public List&lt;Amenities&gt; Decode(long mask)
          {
              var list = new List&lt;Amenities&gt;();
              for (var index = 0; index &lt; 16; index++)
              {
                  var bit = 1 &lt;&lt; index;
                  if (0 != (bit &amp; mask))
                  {
                      list.Add(new Amenities(index));
                  }
              }
              return list;
          }
  </code></pre>
  
  <p>But can not get it to work.
  Any suggestions on how to make this work properly would be appreciated.</p>