LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

shifting an unsigned int by 32bits to the left gives bad result

When I try to shift a 32bit unsigned integer value to the left by 32 bits, I should have a result value of 0. But instead of that, I get back the original content of the shifted variable.

Example code:

unsigned int uiVal = 0x01;
unsigned int uiResult;

    uiResult = uiVal << 32;      //uiResult should have the value of 0, but I get a value of 1.

How it can be?

   

0 Kudos
Message 1 of 3
(3,099 Views)
I have found the answer in ANSI C specification, so sorry for bothering you.
So the answer is:

The result of the shift operation is undefined if the right operand is negative or if its value is greater than the number of bits in an int .
0 Kudos
Message 2 of 3
(3,092 Views)
1) a << b is equivalent to a*(2^b). I don't see why the result shoud be zero.
2) to me, the result is undefined because as far as I remember, one can not shift by a number of bit greater or equal to the number of bit of the left expression. Unsigned int is 32 bit no?
BR

0 Kudos
Message 3 of 3
(3,090 Views)