Getting my self a bit confused here.
I would like to test if a set of bits (3 bits) contains a bit in a certain postion.
if (B110 & B010 == B010)
(B110 being the number to check, B010 the bit I want to see if is there)
The above code isn’t giving me the expected out come, both B110 is true and B101 is true. I am pretty sure that I need to use a &(and) to test with the mask B010.
My understanding is that B110 & B010 would be equal to B010 and that B101 & B010 would equal B000. But my if statement is run with both test bits?
I am coding in an Arduino, I’m sure that it’s a simple misunderstanding on my behalf but not sure where.
Try
if ((B110 & B010) == B010)At the moment it’s running as
if (B110 & (B010 == B010))which will always be true.As this table shows, == and != have a higher precedence than &, | etc.