# Question about how data sent from the tmp102 is processed

I wired up a TMP102 sensor(on a breakout board) from sparkfun. The wiring made sense, standard i2c wiring. Here is the TMP102's datasheet. I used this guide as a guide for the program that I wrote. The program that is running on the particle photon(the microcontroller board that I am using), has a confusing line of code. The line of code that I am referring to does this: The bits of the MSB that are received by the microcontroller(Most significant byte, not bit) are shifted left, 8 times and then Or'ed with the LSB(least significant byte). The bits of the result are then shifted right 4 times. Lastly, the whole thing is multiplied by 0.0625. This line of code sums up what I just said, and can be seen in the guide linked above:

int temp = ((( MSB << 8) | LSB) >> 4) * 0.0625;

I got everything to work(it measures temperature), but I don't know why it works. My question is: why is the above line of code required? Why do these operations need to be completed?. And as a side question, if the bits in a byte are shifted left 8 times, wouldn't that result in a value of 0000 0000? If my question is a bit vague, please tell me.

• An int value is 16 bits, so shifting the received byte left by 8 bits puts that byte in the upper half of the 16 bit int temp. Commented Nov 24, 2016 at 0:31
• To be clear on the size of variables, the preferred definition of temp would be uint16_t temp, clearly declaring it as a 16 bit value. Commented Nov 24, 2016 at 0:54
• So a uint8_t is an integer consisting of 8 bits? I've seen uint_16's in code before and just ignored the number 16. I thought that they were just another way of saying "int x". Thanks for the info. Seems like a basic concept which only a noob like me would overlook... Commented Nov 24, 2016 at 2:01
• Just to clarify (or confuse?), in C, an int must be at least 16 bits, but may be larger if the processor can "naturally" handle larger values, and a long int must be at least 32 bits (If I remember correctly). The uint16_t type notation was introduced to clarify exactly what size variable is required. Commented Nov 24, 2016 at 2:11
• So a uint32_t x = ... is the same as : long x=...? Are these two statements completely equivalent in terms of their behavior and characteristics? Commented Nov 24, 2016 at 2:27

The ( MSB << 8) | LSB) is understood: you want to concatinate the two bytes together. Apparently, in LSB only the [7:4] are valid data. So, you >>4 to get rid of the [3:0] bits. Now, the 0.0625 multiplication is not clear, but it is the same thing as dividing by 16, or performing another >>4 bit shift. The end result in the temp is the MSB only.

MSB: 1010 1010
LSB: 0001 XXXX

MSB<<8     1010 1010 0000 0000
LSB                  0001 XXXX

when ORed
1010 1010 0001 XXXX

when shifted >>4
0000 1010 1010 0001

when multiplied *0.0625
0000 0000 1010 1010=MSB

• I thought concatenation was when you "combine two bits": for example if 0101 was concatenated with 1101 then the result is 01011101? Commented Nov 24, 2016 at 0:16
• So lets say the MSB is 1101 0010 and the LSB is 0110 1011. According to the line of code, the bits in 1101 0010 are shifted left 8 times, so the reult is 0000 0000. This is then OR'ed with the LSB, resulting in a value of (0110 1011). So doesn't this: MSB << 8) | LSB, just give you the LSB? Commented Nov 24, 2016 at 0:20
• Oh, I see the part about concatenation. If the bits in the MSB are shifted left 8 times, the right most 8 bits are all 0's but the left 8 bits are the original MSB. Commented Nov 24, 2016 at 0:22
• Just found some info. On the sparkfun product page it says: "The TMP102 is a digital sensor (I2C a.k.a. TWI), has a resolution of 0.0625°C" Maybe this has something to do with the multiplication of the resulted byte, by 0.0625? Commented Nov 24, 2016 at 0:24
• correct [7:0] is eight bits. this is a common notation. Commented Nov 24, 2016 at 0:32