Hi mates, after a couple of hours I figured out what didn't work. For me it's something illogic, and I'm afraid to loose in future more time on something similar:
Supposing we have a byte IO1_B, the result of the read of I/O chip connected to some lines. I need to test the first bit, ad do something according to it
Why if (IO1_B & 0x1) gives a proper result
and if (IO1_B & 0x1 == 0) or if (IO1_B & 0x1 === 0) doesn't give the opposite result? where is the problem here? Should I writ if (IO1_B & 0x1 === false)? or if ((IO1_B & 0x1) === 0) ?
Thank you
try a google search using javascrippt multiple comparisons
Thank you, unfortunately not easy to find exactly my case..
Bitwise operators return a number.
You can get to grips with them Here
thanks for your answer,, why 0 is not a number?
(IO1_B & 0x1) works, if the first bit of IO1_B is 1 i can execute something, or better.. my goal is to check if it is 0, so I use the "else" command.
now why (IO1_B & 0x1 == 0) doesn't work?
Operator precidance.
Try ((IO1_B & 0x1) == 0)
thank you, now it's more clear how javascript works. In C the precedence is on operators, not on the =, there is no sense to put priority on =
You right however...
== is an operator.
= Is assignment.
I am not absolutely certain but I think you will find that C is exactly the same as javascript in this case, so
a & b == 0
will be interpreted as
a & (b == 0)
in both languages.
Thank you for help! SInce now I always had the feeling that compiler will do everything before == and after it, it has much more logic, by the way it could be just a "good" compiler, to me )
I've got the lesson to always use pharentesis, it will make me much less crazy )
Consider what you might expect if you used
a == 0 & b == 0
You might expect it to interpret this as
(a==0) & (b==0)
In which case ==
has to be performed before &
I understand, I got the reason. Good bless pharentesis