我試圖解碼位掩碼解碼從值在C#中的位掩碼
[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
}
我如何寫,像5722635解碼值,並返回了在5722635編碼的所有設施的列表的功能。
的結果應該是這樣的:
這個屬性有以下設施:
- 商務中心
- 健身中心
- 上網
- 溫泉水療現場
- 嬰兒
- 停車
- 有無障礙通道
- 殘障人士專用浴室
- 游泳池
- 客房內無障礙設施
- 盲文或凸起的標誌
我一直在努力的事情像
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;
}
但不能讓它開始工作。 如何使這項工作正常的任何建議,將不勝感激。
16 ISN」足夠的,而'新'語法來投射可能不起作用 – harold 2014-12-04 13:02:39
新的語法絕對不起作用。你不能創建一個像這樣的枚舉成員的'新'實例 – 2014-12-04 13:06:14