What is the meaning of this line of code

Hi, I have this line of code in a library:

uint8_t mpuIntStatus;

//...some code here that assigns a value to mpuIntStatus

if (mpuIntStatus & 0x10) {

}
else if (mpuIntStatus & 0x02) {

}

What is the meaning of that & ? Wouldnt it be easier to do the following below?

if (mpuIntStatus == 10) {

}
else if (mpuIntStatus == 2) {

}

Some status bits are combined in a variable. the "&" selects the bit of interest and masks out the rest.

huuuummm thanks

Quick read

Ray

Ok, I understood it but I still have a question:

You agree with me that 1001 & 11 = 1, correct? Why does it evaluate to true when I have "if (1001 & 11) {//code}"

0x10 is hex notation for binary 0b00010000. So that line is basically saying "if bit 4 of mpuIntStatus is set" (bit 4 because the right-most bit is bit 0)

That could be replaced with "if ( bitRead(mpuIntStatus, 4) )"

batata004:
Ok, I understood it but I still have a question:

You agree with me that 1001 & 11 = 1, correct? Why does it evaluate to true when I have “if (1001 & 11) {//code}”

In C, any non zero value has a boolean value of “true” and a value of zero has “false”.

batata004: Ok, I understood it but I still have a question:

You agree with me that 1001 & 11 = 1, correct? Why does it evaluate to true when I have "if (1001 & 11) {//code}"

1001 is not the same as binary 1001, when you type if(1001 & 11) you are really asking if binary 1111101001 (which is one-thousand and one) & binary 1011 (which is eleven) and as you can see, notice how bit 0 and 3 in both binary numbers are set (they have a 1). So, the output will be binary 1001, or true

1111101001 & (one-thousand and one)
0000001011   (eleven)
-----------
0000001001   (nine)

//The equivalent is this, and as you can see, 9 is not equal to 0
if(9 != 0) { /*code*/ }

Eleven is 1011 in base 2.

lol...my mistake

My mistake too! I mean B1001 not 1001

batata004: My mistake too! I mean B1001 not 1001

0b1001

0b0011 &

0b0001

1 equals 'true' (As aarg said in reply #6, any non-zero value resolves to 'true'.)

Nice! I didnt know that any non zero value would resolve to true!! Really good to know that