I ran into some trouble with a piece of code. It is supposed to read a 16 bit signed integer over I
2C. The data must be read in two 8 bit chars and then parsed together. Here is the code:
1 2 3 4 5 6 7 8 9
|
int ds1621_read(int sensor, int reg)
{
unsigned char data[2];
// Read data...
return ((int)((short)data[1] + (((short)data[0]) << 8)));
}
| |
The problem is that negative numbers loop around and become positive (i.e. -1 becomes 65535). I know that if I change the return line to:
return ((short)(data[1] + (((short)data[0]) << 8)));
then it works. I think the original turns a negative number into a positive number because it typecasts a 2 byte short into a 4 byte integer and the leading 2 bytes are filled with 0's rather than 1's, and without the leading 1's the int is interpreted as a positive number. My questions are these:
1) Is my assessment right, or did I just stumble across a solution?
2) Is it acceptable to return a short in a function that returns an int?
EDIT:
3) Why, in this case, does implicit typecasting work and explicit typecasting not?
Thanks in advance for any help.