jgmdesign
Level: Moderator
Joined: Mon. Mar 19, 2007
Location: Long Island New York
Posted by jgmdesign:
Tue. Dec 8, 2020 - 06:08 PM
Greets Freaks!
I am combining a two byte ADC output into a single integer. As I do not do much programming anymore I had to think about how to do this....and a little help from teh internet to jog the memory cells into place.
Two examples were:
int i = ((adc_high_byte & 0xFF) << 8) | (adc_low_byte & 0xFF);
Ok, this one looked familiar from projects past.
But then I see this one:
int i = (adc_high_byte << 8) + adc_low_byte;
Which in theory should give me the same result.
Heres the question(s)
Why do I need to AND the byte against a mask THEN shift left in the first example? THe second one seems far simpler(thus faster).
Cheerio!
JIm
This topic has a solution. Jump to the solution.
I would rather attempt something great and fail, than attempt nothing and succeed - Fortune Cookie
"The critical shortage here is not stuff, but time." - Johan Ekdahl
"Step N is required before you can do step N+1!" - ka7ehk
"If you want a career with a known path - become an undertaker. Dead people don't sue!" - Kartman
"Why is there a "Highway to Hell" and only a "Stairway to Heaven"? A prediction of the expected traffic load?" - Lee "theusch"
Speak sweetly. It makes your words easier to digest when at a later date you have to eat them ;-) - Source Unknown
Please Read: Code-of-Conduct
Atmel Studio6.2/AS7, DipTrace, Quartus, MPLAB, RSLogix user
Tags:
Last Edited: Thu. Dec 10, 2020 - 02:59 PM