up in the beginning of the program, there's a char variable declared
as: char values[10];
This is not a “char variable”, it's an “array of 10 chars”.
((int)values[1]<<8)|(int)values[0]
Majenko's answer perfectly explains the intent of the author of the
tutorial, but that is not necessarily what the code actually does. As
it stands, this piece of code is buggy. Or, to say the least, it has a
serious portability problem: it will probably work fine on ARM-based
boards, but it will fail on AVR-based boards, and on x86.
The reason is that, in C and C++, the type char
can be either signed
or unsigned, depending on the platform. On most platforms, including
AVR, it is signed. This implies that, when a char
is converted to
int
, it undergoes sign extension instead of being just zero
padded. To put it graphically, let's assume the sign bit (most
significant bit) of values[0]
is s
, then:
values[0] = sxxxxxxx
(int)values[0] = sssssssssxxxxxxx
Here I am assuming an AVR (16-bit ints). If the sign bit happens to be
zero, this will work as intended. This is the case in Majenko's example.
But if we replace 0x42 with 0xa2, we will have:
values[1] = 0x37 = 00110111
(int)values[1] = 0x0037 = 0000000000110111
(int)values[1]<<8 = 0x3700 = 0011011100000000
values[0] = 0xa2 = 10100010
(int)values[0] = 0xffa2 = 1111111110100010
Then, the bitwise OR gives
0x3700 = 0011011100000000
0xffa2 = 1111111110100010
------------------------- |
0xffa2 = 1111111110100010
and the data in values[1]
is lost.
Conclusion: values
should be declared as an array of unsigned
chars, as this will enforce zero-padding instead of sign extension.
Always use unsigned types when doing bit manipulations. And you probably
should find a better tutorial to learn this kind of stuff.
char values[10]
is actually declaring an array of 10chars
- but none of them are assigned values (presumably the values get set when reading data from the sensor). Those array values are accessed usingvalues[0] ..to.. values[9]
. So when it uses(int)values[1]
it is referring to the secondchar
in the array (and casting it to anint
).