1

I'm using an enum as a flag

 [Flags] public enum SystemStatusEnum : long { Line_seizure = 1 << 0, Off_hook = 1 << 1, Initial_handshake_received = 1 << 2, Download_in_progress = 1 << 3, Dialer_delay_in_progress = 1 << 4, Using_backup_phone = 1 << 5, Listen_in_active = 1 << 6, Two_way_lockout = 1 << 7, Ground_fault = 1 << 8, Phone_fault = 1 << 9, Fail_to_communicate = 1 << 10, Fuse_fault = 1 << 11, Box_tamper = 1 << 12, Siren_tamper_trouble = 1 << 13, Low_Battery = 1 << 14, AC_fail = 1 << 15, Expander_box_tamper = 1 << 16, Expander_AC_failure = 1 << 17, Expander_low_battery = 1 << 18, Expander_loss_of_supervision = 1 << 19, Expander_auxiliary_output_over_current = 1 << 20, Auxiliary_communication_channel_failure = 1 << 21, Expander_bell_fault = 1 << 22, _6_digit_PIN_enabled = 1 << 24, Programming_token_in_use = 1 << 25, PIN_required_for_local_download = 1 << 26, Global_pulsing_buzzer = 1L << 27, Global_Siren_on = 1 << 28, Global_steady_siren = 1 << 29, Bus_device_has_line_seized = 1 << 30, Bus_device_has_requested_sniff_mode = 1 << 31, Dynamic_battery_test = 1 << 32, AC_power_on = 1L << 33, Low_battery_memory = 1L << 34, Ground_fault_memory = 1L << 35, Fire_alarm_verification_being_timed = 1L << 36, Smoke_power_reset = 1L << 37, _50_Hz_line_power_detected = 1L << 38, Timing_a_high_voltage_battery_charge = 1L << 39, Communication_since_last_autotest = 1L << 40, Power_up_delay_in_progress = 1L << 41, Walk_test_mode = 1L << 42, Loss_of_system_time = 1L << 43, Enroll_requested = 1L << 44, Test_fixture_mode = 1L << 45, Control_shutdown_mode = 1L << 46, Timing_a_cancel_window = 1L << 47, Call_back_in_progress = 1L << 55, Phone_line_faulted = 1L << 56, Voltage_present_interrupt_active = 1L << 57, House_phone_off_hook = 1L << 58, Phone_line_monitor_enabled = 1L << 59, Sniffing = 1L << 60, Last_read_was_off_hook = 1L << 61, Listen_in_requested = 1L << 62, Listen_in_trigger = 1L << 63, } 

Here I declare my flags =

SystemStatusEnum SystemStatusFlags = SystemStatusEnum.Off_hook | SystemStatusEnum.Phone_fault | SystemStatusEnum.Expander_bell_fault | SystemStatusEnum._6_digit_PIN_enabled | SystemStatusEnum.Timing_a_high_voltage_battery_charge | SystemStatusEnum.Loss_of_system_time | SystemStatusEnum.Call_back_in_progress | SystemStatusEnum.Phone_line_faulted; 

Here I convert the enum to 8 bytes

 // System Status Flags Byte[] SystemStatusBytes = new Byte[8]; SystemStatusBytes[0] = (byte)((long)SystemStatusFlags & 0xff); SystemStatusBytes[1] = (byte)(((long)SystemStatusFlags >> 8) & 0xff); SystemStatusBytes[2] = (byte)(((long)SystemStatusFlags >> (8 * 2)) & 0xff); SystemStatusBytes[3] = (byte)(((long)SystemStatusFlags >> (8 * 3)) & 0xff); SystemStatusBytes[4] = (byte)(((long)SystemStatusFlags >> (8 * 4)) & 0xff); SystemStatusBytes[5] = (byte)(((long)SystemStatusFlags >> (8 * 5)) & 0xff); SystemStatusBytes[6] = (byte)(((long)SystemStatusFlags >> (8 * 6)) & 0xff); SystemStatusBytes[7] = (byte)(((long)SystemStatusFlags >> (8 * 7)) & 0xff); for (int i = 0; i < SystemStatusBytes.Count(); i++) { MessageBytes[Index++] = SystemStatusBytes[i]; } 

And here I convert them back to a list

 private void ParseSystemStatusFlags(int Flag1, int Flag2, int Flag3, int Flag4, int Flag5, int Flag6, int Flag7, int Flag8) { long flagsRaw = (long)(Flag1 | Flag2 << 8 | Flag3 << (8 * 2) | Flag4 << (8 * 3) | Flag5 << (8 * 4) | Flag6 << (8 * 5) | Flag7 << (8 * 6) | Flag8 << (8 * 7)); SystemStatusEnum flags = (SystemStatusEnum)flagsRaw; SystemStatus_List = flags.GetFlags().ToList(); } 

But I'm getting an unexpected result, I get Off_hook,Two_way_lockout,Phone_fault,Fuse_fault,Expander_bell_fault,_6_digit_PIN_enabled as a result.

So some flags are correct, but Two_way_lockout for example is not declared in my enum.

What am I doing wrong?

2 Answers 2

1

I suspect this may partly be a precedence issue between << and |, and partly be a problem of shifting int values instead of long values. Try this:

long flagsRaw = ((long) Flag1 << 0) | ((long) Flag2 << 8) | ((long) Flag3 << (8 * 2)) | ((long) Flag4 << (8 * 3)) | ((long) Flag5 << (8 * 4)) | ((long) Flag6 << (8 * 5)) | ((long) Flag7 << (8 * 6)) | ((long) Flag8 << (8 * 7)); 

I haven't checked the precedence rules to see whether that's the problem, but at the very least the above is clearer in its intention.

Sign up to request clarification or add additional context in comments.

2 Comments

Now I get a different result, I get the result I want, but also Bus_device_has_requested_sniff_mode.
Ok, I found the problem. I edited my code to your snippet and changed every 1 in enum to 1L and now it works.
0

Long is signed type so you can actualy use only 64 - 1 bit, Listen_in_trigger = 1L << 63 that is not correct

1 Comment

No, you can use all 64 bits - it's just that 1L << 63 is negative.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.