I have a variable that have 2x int16 values as:
int32_t value = 0x1234ABCD; // a=0x00001234, b=0xFFFFABCD
The naive solution is to do a mask:
int32_t a = (value & 0xFFFF0000) >> 16;
int32_t b = (value & 0x0000FFFF);
But with this, I don't have any sign expansion and b becomes 0x0000ABCD
instead of 0xFFFFABCD
.
My next attempt was to use an intermediate structure
struct dual_int16 {
long hi:16;
long lo:16;
}
int32_t a = (struct dual_int16)value).lo;
int32_t a = (struct dual_int16)value).hi;
Unfortunately my compiler doesn't allow me to do this "struct dual_int16 is not allowed" or "type of cast must be arithmetic or pointer".
Is there any correct way to extract my 2x int16 with sign expansion in C99?
EDIT
Because I am using a specific compiler (ADSP-21xxx) . I don't have all the standard types defined in stdint.h
such as int16_t
. My compiler does not recognize int8_t
and int16_t
.
The arch has an hybrid 32-48bits dual ALU, big endian.