We are running a survey to help us improve the experience for all of our members. If you see the survey appear, please take the time to tell us about your experience if you can.
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!
it depends on what you are trying to do:
(a!=b) && 0xff or a!=(b&0xff)
it is always helpful to use brackets to enhance readability and ensure the desired results.
Both should yield the same result, shouldn't they?
(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