I accidentally trigger a bug in C code, my goal is to get rightmost 16 bits as unsigned from binary string.
#include <stdio.h>
#include <stdint.h>
int main()
{
char msg[] = {0x01, 0x02, 0x03, 0x04, 0x11, 0xFF};
uint16_t val;
#define SIZE 6
val = (msg[SIZE-2] << 8) | msg[SIZE-1];
printf("%u\n", val);
return 0;
}
Above code will produce val=0xFFFF as a result(GCC), to fix, I need to change msg from signed char to unsigned char. But my question is why this will happen and what memory is doing?