How to write code to test if the bit is 1 or 0

found unexpected result when i write the following code to test if the highest bit is 1 :
byte i=0x7F; if (i&0x80==0){}else{};
I expect the result is true, but when I compile and run, it is false.

I read the reference about the language, and I tried to force the type to byte, if ((byte)i&0x80==0), also not I expected.

Why and how to solved the issue?

when I tried the following code:
byte i=0x7F,j; j =i&0x80; if (j==0){}else{};
It works as I expected, why?

First, please use code tag.

Comparison operators are evaluated before bitwise operators.

if ( ( i&0x80 ) ==0){}else{};
//   ^        ^
//   need add this
2 Likes

It works! Thanks a lot!

That one catches me occasionally, and I've been programming for almost 50 years.

Got you beat- I started with Fortran 2.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.