09-04-2008 07:39 AM - edited 09-04-2008 07:41 AM
Hi,
I discovered a -what I think- strange behaviour of my CVI 8.5.1 compiler and I wanted to ask your comments about it.
Try this in interactive execution window (it will result in a pass popup as expected):
#include <userint.h>
static unsigned short w = 0x8101;
if (w & 0x1800) { MessagePopup ( "fail", "fail" ); }
else { MessagePopup ( "pass", "pass" ); }
Now try this:
#include <userint.h>
static unsigned short w = 0x8101;
if (w & 0x1800 != 0) { MessagePopup ( "fail", "fail" ); }
else { MessagePopup ( "pass", "pass" ); }
Did it say fail? Well, that what happens to me.
Casting 0x1800 or 0 to unsigned short does not work.
I could only make the second snippet pass only by changing w from 0x8101 to 0x8100 ?!
How in the world can the result of these two operations be different : (0x8101 & 0x1800) ; (0x8100 & 0x1800).
By the way clearing other bits of 0x8101, I mean changing it to 0x0101 or 0x8001 does not help either.
The behaviour is the same for versions 8.0.1 and 6.0.
Is this a C language issue that I am not yet aware of or a compiler "bug"?
Solved! Go to Solution.
09-04-2008 07:55 AM - edited 09-04-2008 07:56 AM
In C, the operator != has higher precedence than &, so the expression is evaluated as:
w & (0x1800 != 0)
which is true if w=0x8101.
09-04-2008 09:56 AM
Wow! I never thought of an operator precedence issue in this matter.
Thanks a lot.
I knew I was making a dummy mistake, but could not figure out what