LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Bitwise negation operator problem

The attached source and project files highlight a problem in the unary bitwise negation operation (~) when used in "if" statements without explicit assignments to a target variable.

The code has been run under CVI 5 5.x.
What is the result of the test under CVI 6.0?
If you compile the module with Borland C++ Builder 5.0 the behavior seems to be correct.

Is this a problem in the CVI compiler or there are "holes" (implementation-depended features) in the ANSI C "standard"?

I've browsed the book "C - A Reference Manual", fourth edition by Harbison-Steele. According to what is written at page 197 (paragraph 7.5.5), the result of this operator should be portable if you use unsigned operands.

Kind Regards
Download All
0 Kudos
Message 1 of 2
(3,132 Views)
It is possible that CVI may be performing an unforseen automatic conversion of integer datatypes (char, unsigned char, short, long, etc...) into longs when certain operations take place such as unary bitwise negation. However, ANSI C does not define exactly how compilers are supposed to implement mathematical, logical, and data conversion operations. In fact it is very compiler implementation dependent, and there very well may be a dozen different implementations of these operations on the market.

In this case you expected the char variable to be treated exactly as it was defined with no automatic casting done, but if this is observed to not be what is actually happening then you are always recommended in most compilers to explicitly cast your types to match the
operation you want performed.

For example place the following casts on the data you wish to use:

~(char)mychar;
~(short)myshort;
~(unsigned short)myunsignedshort;

Jason F.
Applications Engineer
National Instruments
www.ni.com/ask
0 Kudos
Message 2 of 2
(3,132 Views)