Re: regarding type promotion

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

 



Hi Siva,

> If we consider following sample C language code :
> 
> long long int k=0x123;
> int p=1;
> k = k + p << 33;
> 
> Here the value in variable 'p' is shifted by 33 and then the result
> (only 32 bit result)was promoted to 64 bit.

Yes, that's correct.

((k) = ((k) + ((p) << (33))));

Using L to mark long long, and I to mark int, and @ to mark promotion:

L(L(k) = L(L(k) + L@(I(I(p) << I(33)))));

Also note that where int is 32-bit, doing a i << 33 is undefined behavior.
It could shift to zero.  It could leave the number as-is.  It could shift by
one.  It could... DESTROY THE UNIVERSE.  So be careful with that.

(If shifting a 32-bit int by 33 with GCC does destroy the universe, please
file a bug <http://gcc.gnu.org/bugs.html>.)

> Is it an expected behavior?

Yes.

> Is there any way to specify in gcc to perform implicit type promotion
> first and then perform operation on it (without explicit type casting).

Yes.

long long k=0x123; // <-- implicit promotion 0x123 --> 0x123LL
long long p=1; // <-- implicit promotion 1 --> 1LL
k = k + p << 33;

HTH,
--Eljay


[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