Re: Bitwise shift operator on 8-byte integers

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxx 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


[Index of Archives]     [Linux C Programming]     [Linux Kernel]     [eCos]     [Fedora Development]     [Fedora Announce]     [Autoconf]     [The DWARVES Debugging Tools]     [Yosemite Campsites]     [Yosemite News]     [Linux GCC]

  Powered by Linux