Let's say I have an enum that only contains values up to 0xFF. I want to place that enum value into a byte variable. AFAIK enums are int
s underneath (vxWorks). The machine I am coding for is big endian. I don't understand if this matters when I want to place it into a uint8_t. Which of these options will work and which is the best?
typedef enum {
kEnum1 = 0x00,
kEnum2 = 0x01,
kEnum3 = 0xFF
} MyEnum;
MyEnum myenum = kEnum3;
uint8_t mybyte = 0;
// option 1, do nothing
mybyte = myenum ;
// option 2, explicit cast
mybyte = (uint8_t)myenum;
// option 3, use operators
mybyte = (myenum & 0xFF000000) >> 24;
When I test this on an online compiler, which I assume is little endian, option 3 of course does not work. But the others do. I just don't understand how big endian machines will treat these options.