This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

a!=b&0xff

unsigned char a=0x12; unsigned char b=0x22; if(a!=b&0xff)//is not true,but why? a = 0; else b = 0;//execute this

"a!=b" is true ,so “true &0xff ”should be true,I want to know the c51 compiler why do this?

Thanks!

Parents Reply Children
  • (a!=b) && 0xff is always (a!=b)

    a!=(b&0xff) is (a!=b) if b is a char type.

    for cases where b is not a char type (short or long or float/double), the 2nd test may not yield the same results as the 1st one.

    for example, a=0x22, and b=0x2222.

    the first test is true (thus a doesn't equal to b).

    the 2nd test is false, since b&0xff=0x22 so a=b&0xff, thus the 2nd test is false.

    that obviously doesn't hold here as both a and b are char.

    my point back then was that it always helps with readability if you bracket the expressions - I am too old to remember every rule of priorities.

  • "a!=(b&0xff) is (a!=b) if b is a char type."

    more accurately, a!=(b&0xff) is (a!=b) if b is between 0x00 and 0xff