LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

bitwise AND with comparison

Solved!
Go to solution

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"?

Message Edited by ebalci on 09-04-2008 03:41 PM
S. Eren BALCI
IMESTEK
0 Kudos
Message 1 of 3
(3,163 Views)
Solution
Accepted by topic author ebalci

In C, the operator != has higher precedence than &, so the expression is evaluated as:

 

w & (0x1800 != 0)

 

which is true if w=0x8101.

 

 

 

Message Edited by msaxon on 09-04-2008 01:56 PM
--
Martin
Certified CVI Developer
Message 2 of 3
(3,160 Views)

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 Smiley Wink

S. Eren BALCI
IMESTEK
0 Kudos
Message 3 of 3
(3,145 Views)