--------------------------------------------------------------------------------
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?
Mostly it's left to right, but for some operators, it's right to left, the rules can be found in your compiler docs.
It's not order in your case, + and - come before << >> no matter what order there in.
Is the rest of the conversion code interesting to you? I didn't post it since it isn't relevant to my question. The conversion is for a 16 bit sign-extended two's complement word from a DS18B20 temp sensor. I convert to a float. Let me know if you want that particular code snippet.