Best to use & or &&)

this->_isDetectingRaw = this->_isEnabled && digitalRead (this->_sensorPIN ) ^ this->_sensorPINisInverted;

_isDetectingRaw and _isEnabled are of type bool.

I do not think it makes any difference if I use two ampersands as in the example above or one. The result should be the same. Am I correct?

How do you bitwise AND two bools?

The single ampersand is the bitwise operator. The double ampersand can be (and in my view should be) replaced with the word and for greater clarity.

Use the correct operator for the action that you need. Using the wrong operator, even if it gives the same result, will just make the code confusing. Code should be written so it is easy to understand - not so it looks clever.

...R

TheMemberFormerlyKnownAsAWOL:
How do you bitwise AND two bools?

In my thinking, I was comparing the first (rightmost, lowest value) bit of the bool with the boolean output of the digitalRead(). So the bitwise AND operator would do the job perfectly.

Robin2:
The single ampersand is the bitwise operator. The double ampersand can be (and in my view should be) replaced with the word and for greater clarity.

Use the correct operator for the action that you need. Using the wrong operator, even if it gives the same result, will just make the code confusing. Code should be written so it is easy to understand - not so it looks clever.

...R

I did not even know that it was possible to use words like 'and', or 'or' even 'xor'. But they indeed shift in colour when entered in the IDE. They are not mentioned on the page Arduino Reference - Arduino Reference. Feels a bit like BASIC. :slight_smile:

teddyz:
But they indeed shift in colour when entered in the IDE.

Don't worry about the colours in the IDE - they are just decoration and are not always correct. If you get some syntax wrong the compiler will let you know.

Also the Arduino uses standard C++

...R

teddyz:
In my thinking, I was comparing the first (rightmost, lowest value) bit of the bool with the boolean output of the digitalRead(). So the bitwise AND operator would do the job perfectly.

You’re not supposed to know/care about how types like bool are implemented internally.

You’ll end up chasing seemingly crazy stuff like:

int a = 5;
int b = 10;

Now, “a” is true, “b” is true, “a && b” is true, but “a & b” is false.

Another thing is that & and && (also, | and ||) have very different evaluation semantics.

For & and |, both operands are always evaluated.

For && and ||, the second operand is evaluated only if necessary. If the first operand to && is false, then the second is NOT evaluated. Similarly, if the first operand to || is true, the second is not evaluated.

You can look up 'short circuit evaluation' for more details if you like.

Finally, as noted above, people 'expect' that & and | will be used for bit-twiddling, and && and || will be used for boolean logic. The language does not prohibit it, but misusing these operators is misleading to anyone else reading the code.

Thanks for your answers!

I was thinking I could save a few clock cycles by using &, but now I see that there are more advantages using &&.

I searched for " and " and found it in 330 .cpp-files in my library collection. But only in one of these (UTFT.cpp) was it used in the code.

teddyz:
now I see that there are more advantages using &&

the question should not be about "advantages" but the semantics of your code. if you are doing a bitwise AND, use & whilst if you are doing a boolean logical AND then use &&.

J-M-L:
the question should not be about "advantages" but the semantics of your code. if you are doing a bitwise AND, use & whilst if you are doing a boolean AND then use &&.

I have now known another vocabulary -- the 'boolean AND' in addition to my long known vocabulary 'logical AND'.

Actually you are right @Golam, it's called a logical AND (or logical conjunction), not boolean

Prototypical Boolean algebra does exist and introduces operations, such as AND (conjunction), OR (disjunction), NOT (negation) but they don't really carry the name of "boolean AND" etc...