& operators into if instrucion

#1

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

0 Likes

#2

try a google search using javascrippt multiple comparisons

0 Likes

#3

Thank you, unfortunately not easy to find exactly my case..

0 Likes

#4

Bitwise operators return a number.

You can get to grips with them Here

0 Likes

#5

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?

0 Likes

#6

Operator precidance.
Try ((IO1_B & 0x1) == 0)

0 Likes

#7

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 =

0 Likes

#8

You right however...
== is an operator.
= Is assignment.

0 Likes

#9

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.

0 Likes

#10

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 )

1 Like

#11

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 &

0 Likes

#12

I understand, I got the reason. Good bless pharentesis

0 Likes