I am trying to decode a bitmask
[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
}
How do I write a function that decodes a value like 5722635 and returns a list of all Amenities that are encoded in 5722635.
the result should look like this:
This Property has the following Amenities:
- Business Center
- Fitness Center
- Internet Access
- Available Spa On-site
- Babysitting
- Parking
- Accessible Path of Travel
- Accessible Bathroom
- Roll-in Shower
- In-room Accessibility
- Braille or Raised Signage
I have been trying things like
public List<Amenities> Decode(long mask)
{
var list = new List<Amenities>();
for (var index = 0; index < 16; index++)
{
var bit = 1 << index;
if (0 != (bit & mask))
{
list.Add(new Amenities(index));
}
}
return list;
}
But can not get it to work. Any suggestions on how to make this work properly would be appreciated.
new
syntax to cast probably doesn't work – Colum