programming question

Post here about your Arduino projects, get help - for Adafruit customers!

Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.
Locked
ErikT
 
Posts: 3
Joined: Fri Dec 16, 2011 11:48 pm

programming question

Post by ErikT »

I'm trying to understand the code in the DHT.cpp library (https://github.com/adafruit/DHT-sensor- ... er/DHT.cpp).

There are a couple of lines that I don't understand. What is 0x7F as seen here?
f = data[2] & 0x7F;

And what is 0x80 as seen here?
if (data[2] & 0x80)

User avatar
westfw
 
Posts: 2010
Joined: Fri Apr 27, 2007 1:01 pm

Re: programming question

Post by westfw »

Those are hexidecimal (base 16) constants. "0x" is a prefix used by C (and several other languages) to indicate a hex constant rather than the usual decimal numbers. Hex can make particular bit-patterns more obvious.
0x7F is 127 (01111111 base 2), 0x80 is 128 (10000000 base 2.) The statements set f to the lower 7 bits of the data, and then does something based on whether the high bit was set or not.

Binary/hexadecimal numbers and alternate number bases are fundamental to understanding how computers do things at the very "bottom" levels, but are a pretty big leap if you've never been exposed to them before.

Locked
Please be positive and constructive with your questions and comments.

Return to “Arduino”