NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Microcontroller Programming » Coding question

January 13, 2011
by tomroth
tomroth's Avatar

In the tempsensor program, when reading from the ADC, there are 2 variables. One is "result" and one is "temp". The comments say to read the lower bits first, then the high bits and add them together. The code was:

result = result + (temp<<8);

My question is why the (temp<<8)? Isn't that an 8 position left shift? I'm a bit confused. Thanks for the help!

Tom

January 13, 2011
by bretm
bretm's Avatar

There are 8 lower bits. The left shift is to move the upper bits up past the lower bits.

If you think in base 10 instead of binary, let's say someone gave you the number 12345 in two chunks, the lower digits, 345, and the upper digits, 12. To combine them together, you need to shift the 12 three digits over to get 12000, then when you add the 345 you get the full number, 12345.

Same thing in binary. Left shift is just putting zero's on the end to make room for adding the lower bits.

Post a Reply

Please log in to post a reply.

Did you know that Pulse Width Modulation (PWM) can be used to control the speed of a motor digitally? Learn more...