Elsewhere it has been explained that the !! double negation ensures that a 0 returns a 0 and any non-zero value returns a 1. That makes sense, but wouldn't the Bitwise AND function do that if we AND with a 1 anyway? What benefit does the extra double negation give in this situation?
Ahh, That makes perfect sense, no matter what the outcome of the bitwise AND was, there will always be a 1 in Bit 0, which is the bit we are sending to the pin
Thanks and big ups for a perfect explanation. While in the particular case we are only testing for low, it is resilient for the more general case.
And which is somewhat semantically flawed as sending true or false should not be considered (if you are a purist) the same as sending HIGH or LOW
It works because if the operand is not bool, it is converted to bool using contextual conversion (standard C) and by (relatively) arbitrary convention and luck because the authors have selected the same value as true and false for HIGH and LOW)
If you care, this should be written as condition ? HIGH : LOW.
J-M-L:
and luck because the authors have selected the same value as true and false for HIGH and LOW)
I consider it more good design than luck.
J-M-L:
If you care, this should be written as condition ? HIGH : LOW.
Of course that causes the compiler to generate extra code to translate any non-zero value to one. Fortunately for me I'm not a purist and can understand:
int inputState = digitalRead(InputPin) == HIGH ? LOW : HIGH;
digitalWrite(OutputPin, inputState == LOW ? HIGH : LOW);
I wonder if the compiler is smart enough to generate the same code for both. I assume it will at least store a 'boolean' in a byte but an 'int' as two bytes, even though it is used for 1/0 values.
This code fancifully fixes its own oversight - The effort to shift the bit to the LSB arises because it is not in the "right" position. This can be avoided by shifting the tested bit into the LSB instead, like: