1

I'm using an enum as flags:

[Flags]
public enum Partition_Status_ConditionEnum : long
{
    Bypass_code_required = 1 << 0,
    Fire_trouble = 1 << 1,
    Fire = 1 << 2,
    Pulsing_Buzzer = 1 << 3,
    TLM_fault_memory = 1 << 4,
    Reserved = 1 << 5,
    Armed = 1 << 6,
    Instant = 1 << 7,

    Previous_Alarm = 1 << 8,
    Siren_on = 1 << 9,
    Steady_siren_on = 1 << 10,
    Alarm_memory = 1 << 11,
    Tamper = 1 << 12,
    Cancel_command_entered = 1 << 13,
    Code_entered = 1 << 14,
    Cancel_pending = 1 << 15,

    Silent_exit_enabled = 1 << 17,
    Entryguard = 1 << 18,
    Chime_mode_on = 1 << 19,
    Entry = 1 << 20,
    Delay_expiration_warning = 1 << 21,
    Exit1 = 1 << 22,
    Exit2 = 1 << 23,

    LED_extinguish = 1 << 24,
    Cross_timing = 1 << 25,
    Recent_closing_being_timed = 1 << 26,
    Exit_error_triggered = 1 << 28,
    Auto_home_inhibited = 1 << 29,
    Night_mode = 1 << 31,

    Re_exit_active = 1 << 32,
    Force_arm_triggered_by_auto_arm = 1 << 33,
    Ready_to_arm = 1 << 34,
    Ready_to_force_arm = 1 << 35,
    Valid_PIN_accepted = 1 << 36,
    Chime_on = 1 << 37,
    Error_beep = 1 << 38,
    Tone_on = 1 << 39,

    Entry_1 = 1 << 40,
    Open_period = 1 << 41,
    Alarm_sent_using_phone_number_1 = 1 << 42,
    Alarm_sent_using_phone_number_2 = 1 << 43,
    Alarm_sent_using_phone_number_3 = 1 << 44,
    Zone_bypassed = 1 << 45,
    Keyswitch_armed = 1 << 46,
    Delay_Trip_in_progress = 1 << 47,
}

But when I add them to a list

   List<Partition_Status_ConditionEnum> PartitionConditionList = new List<Partition_Status_ConditionEnum>();
   PartitionConditionList.Add(Partition_Status_ConditionEnum.Valid_PIN_accepted);
   PartitionConditionList.Add(Partition_Status_ConditionEnum.Cancel_pending);
   PartitionConditionList.Add(Partition_Status_ConditionEnum.Exit2);
   PartitionConditionList.Add(Partition_Status_ConditionEnum.Auto_home_inhibited);
   PartitionConditionList.Add(Partition_Status_ConditionEnum.Error_beep);

   Byte[] Message = Msg.BuildMessage(true, PartitionNumber, LastUserNumber, PartitionConditionList);

Valid_Pin_Accepted is seen as TLM_Fault_Memory and Error_Beep as Armed. What am I missing?

enter image description here

4

3 回答 3

5

Use long constants: 1L << 36.

http://ideone.com/3Bwbky

于 2013-06-20T12:46:53.167 回答
4

Your literals are 32 bit, so 1 << 42, for example, is a 32 bit number and the same as 1 << (42 - 32). Hence some of your enums have duplicate values.

To circumvent this behaviour, promote one of the literals to a long type: 1L << 42 will do it.

于 2013-06-20T12:48:18.593 回答
0

If you are trying to do a bit-wise representation, then you don't need to use a list. You need to use the binary OR operation. You should change to:

PartitionCondition = Partition_Status_ConditionEnum.Valid_PIN_accepted | 
   Partition_Status_ConditionEnum.Cancel_pending |
   Partition_Status_ConditionEnum.Exit2 | 
   Partition_Status_ConditionEnum.Auto_home_inhibited |
   Partition_Status_ConditionEnum.Error_beep;
于 2013-06-20T12:47:58.437 回答