Casting float to unsigned short
Hi everyone, I've got a strange bug with my system. I have a float struct member which I divide with a float resolution value. Then I cast it to an unsigned short so that I can put it in a two bytes message. Like this: float calc, num = -150.35, res = 0.703125; unsigned short val; val = (unsigned short)(num / res); msg_1 = val >> 8; msg_2 = val; The problem here, with a positive num variable, I cast it to the val successfully; with a negative one, val is 0 no matter what is the num's value. I couldn't find out why. But when I changed the type of val to signed short, I managed to calculate the result successfully for both of the positive and the negative values. But I'm not sure if this is healthy for messages. I usually use unsigned short and unsigned char without knowing why.