Bitwise shift operator on 8-byte integers

Sisyphus kalinabears@iinet.net.au
Wed Oct 6 06:53:00 GMT 2004


Ankit Jain wrote:
> hi Eljay,
> 
> I understand the solution given by u and its workign
> is also fine . but could not understand why this is
> not working
> 
> thanks
> 
> ankit
>  --- Krzysztof.Wisniowski@siemens.com wrote: 
> 
>>Hallo *, 
>>Here's the problem:
>>
>>unsigned int ui = 4294967295; //2^16-1

Actually, that's 2^32-1.

>>unsigned long long uL; //8-byte variable
>>
>>uL = ui << 16;

You're asking that a 32 bit value (ui) be left shifted 16 places. This 
means that the 16 high bits will be discarded. The result of that left 
shift will then be assigned to uL.

I believe that another solution would be to write it as:
uL = (unsigned long long)ui << 16;

this casts ui to an unsigned long long - so we're now asking that a 64 
bit value be left shifted 16 places - and no bits will be lost. 
Consequently uL will contain the value you expect.

Get the picture ?

Cheers,
Rob



More information about the Gcc-help mailing list