@Dana: why "lesser pillar"? Just because I am for 5 minutes on place# 1 and you on place# 2?? That's going to change back within a matter of hours...
Bob, I did not know there is a # 1 position. Wow, does one get crowned, robes, jewel encrusted
shoes ? I am so looking forward to Medieval that. How does one find out if they are # 1, error rate,
knowledge, customer interface.......
Should this discussion really be in a customer request for assistance thread ? Should we be apologizing
to Chris for inappropriate thread content ? I think so. My apologies Chris, I will stay on topic.
Dana, I did not name you "Lesser Pillar of Knowledge" and I was questionig why you did that. And maybe I've forgotten to put a smiley onto that post, forgive me for getting off the point (which is undoubtfully the forgotten "&&")
Hmm,. Did either of you actually try to run the code as written. Because it works. I was not asking for your corrections just that you try to verify the results that I was seeing.
No, I did not run the code. For me it is quite clear that the code behaves exactly as you described. the double "if" for the second part does exactly what you wanted to do, it replaces a logical "AND". So what question remains for you?
The original question was why does the first if statement with the two parts "&"ed (46) work when exactly the same type of expression later (55) will not work. I must separate the parts into two if statements at (55) for a sucessful step.
I tried to explain the difference between "&" and "&&"
The "single" operators are named "bit wise" operators and that is exactly the way they work on bytes, words and so on. They do not work as the "double" operators which are named "logical" operators and they are working on boolean variables which are usually 8 bit values. Apart from the "double" operators there exist two NOT operators: The logical "!" and the bit-wise "~".An Example: !5 is equal to 0x00 (because 5 is true per definition and !TRUE is equal to FALSE, which is 0 per definition) while the expression ~5 is equal to ~0b000 0101 which is 0b1111 1010.
So coming back to your original statements
The expression (~PRT0DR & 0x02) will be handled bit-wise and will give the result 0x00 or 0x02.
The expression (Power > 0) is a logical expression and will deliver 0x00 (false) or 0x01 (true)
Now you perform a bit-wise AND of these two values which will ALWAYS result in 0x00 which is false per definition.
0x02 bit-wise ANDed with 0x01 will always be 0x00
When you use the logical operator "&&" the situation looks different:
0x02 is per definition true, anded with another value of "true" will give the result you expect to happen.
OK. Its the ~ that confused me. From my reading I determined that the ~ would deliver the conjugate of the expression. If the expression was TRUE then in the conjugate we would need a FALSE to satisfy the expression. I was not aware that the ~ also forced a bitwisw evaluation of the expression.
And i have just run a quick test to satisfy myself that I understand.
I could use the ~ and the && or I could use the ! and the &. Both formats work equally as well in the test project.
There is another common pitfall I have seen rather often:
Since FALSE is defined to be 0 (zero) TRUE is defined to be !FALSE (which both you may #define yourself)
So if(b == FALSE) will always run as expected, but
if(b == TRUE) will not. Even the equivalent to the first if(!(b == TRUE)) will go amiss.
The reason for this is that the compiler has an internal representation of TRUE (which is usually 1) Comparing any value to TRUE will compare it to 1 which usually is not what you want.
if(b) questions b !=0 (as the definition for TRUE and FALSE is) and can easily be used without the compareision with "=="