LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

Please explain CVI Issue #335358: Using the bit shift operator twice on the same line of code produces incorrect results

Solved!
Go to solution

Hi,

 

I just stumbled across http://www.ni.com/white-paper/12323/en#335358_by_Category.

 

Using the bit shift operator twice on the same line of code produces incorrect results.
Workaround: Separate the two bit shift operations into two separate lines of code.

Reported Version: 9.0    Resolved Version: N/A    Added: 02/14/2012

 

Could someone please explain what kind of construct causes such an issue?

I haven't noticed any problems yet and haven't noticed or found a thread about this.

 

Thanks.

-----------------------
/* Nothing past this point should fail if the code is working as intended */
0 Kudos
Message 1 of 5
(3,783 Views)

Does have anyone any information?

From quick search I found over 150 lines with multiple bit shift operators on one line in our source codes, and some of this is in macro definition.

Lot of this code has been developed originaly with CVI7.1 and near 30% of this has been recompiled by CVI2010.

I do not see any problem yet.

0 Kudos
Message 2 of 5
(3,765 Views)
Solution
Accepted by topic author CVI-User

The reported scenario looks like this:

 

unsigned int a = 1, b = 62;
unsigned  long long result = 1ULL << a << b;

 

The problem occurrs because of the temporary variable used when doing two bit shifts in the same line. Separating this into two lines will produce the correct results:

 

unsigned int a = 1, b = 62;
unsigned  long long result = 1ULL << a;
result = result << b;

 

National Instruments
0 Kudos
Message 3 of 5
(3,750 Views)

Thank you D Biel,

               According to your message and some of my quick tests, it seems to me ,that the problem occur when shift operator is inside another shift operator(on left side) and value is 64bit number.
               However, code like (1ULL << a) +(1ULL<< b) works well, which cover most of my ussage cases (of shift operator). I have similar code on only 3 places,but not with 64bit number so it is fine.

 

               It would be nice if you can change text in known issue list with better description and/or add example so there be less panic for any reader.

0 Kudos
Message 4 of 5
(3,735 Views)

Thanks.

-----------------------
/* Nothing past this point should fail if the code is working as intended */
0 Kudos
Message 5 of 5
(3,727 Views)