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,166 Views)

I think you will find that a shift operation must be less than the number of bits in the word being shifted, or else the result is undefined. So for a 32 bit integer, you cannot shift by more than 31. Can't lay my hands on the C documentation right now - relying on my memory. (A risky strategy...)

JR

0 Kudos
Message 2 of 3
(3,163 Views)

Hmm.. I'm going to have to start reading these messages from newest to oldest, instead of the other way round. That's twice today I've been caught out replying to duplicated posts which already have perfectly good replies to the later version.

JR

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