Let's say I have a dword.
uint32_t var1 = 0xABCD1234;
I need to get the upper word as it's own value. There are several different ways to do this in C. Which one is most likely to compile most efficiently on a 32-bit processor/OS? Does it make a difference if it's a 64-bit processor/OS?
1. Shifting
uint16_t var2 = var1 >> 16;
2. Casting
This requires knowing the endianess of the processor, so it's a negative in that regards, but assume you have the endianess correct.
uint16_t var2 = *( (uint16*)&var1 );
or
uint16_t var2 = *( (uint16_t*)&var1 + 1 );
3. Dividing
uint16_t var2 = (uint16_t)( var1 / ( 1ULL << 16 ) );
4. Anything else?
Did I miss any other way to do it?
EDIT: Yes, I missed the union.
union { uint32_t v32; uint16_t v16[2]; } u;
uint32_t var1 = 0xABCD1234;
u.v32 = var1;
uint16_t var2 = u.v16[1]; // or u.v16[0] depending on endianess