Fearsome
Newbie level 6
--------------------------------------------------------------------------------
I have a routine that converts from two's complement to a float. The first thing I want to do is combine the most significant byte and least significant byte of the output of a sensor to one unsigned int. I may not be experienced in C but I thought I did understand type conversions. Here's a part of the code:
What I wanted to do was to convert MS to an unsigned int and left shift it and then add LS. From my understanding of integer promotion, the type cast of LS shouldn't even be needed. However, this conversion doesn't work! What am I doing wrong?
Fred
I have a routine that converts from two's complement to a float. The first thing I want to do is combine the most significant byte and least significant byte of the output of a sensor to one unsigned int. I may not be experienced in C but I thought I did understand type conversions. Here's a part of the code:
Code:
float ConvTwosComp(unsigned char LS, unsigned char MS) {
unsigned int temp;
temp = ((unsigned int) MS)<<8 + (unsigned int) LS;
//...More code that actually converts temp to float.
}
What I wanted to do was to convert MS to an unsigned int and left shift it and then add LS. From my understanding of integer promotion, the type cast of LS shouldn't even be needed. However, this conversion doesn't work! What am I doing wrong?
Fred