This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Re: Integral conversions in C/C++


Tom St Denis wrote:

What type is the expression -(b * 2u)?

It _should_ be that of the destination type of the whole expression. and thus integral conversion of an rvalue applied.

And why would the number 2^32 - 16 be sign extended when stored in
> a 64-bit signed int?

2^32 - 16 is not what -(b * 2u) expresses.  The result of x = b * 2u
can _not_ possibly be stored in a 32 bit object.  Consequently, the
type of its result x should be a type that can contain all possible
values of (b * 2u) (which uint32_t clearly can not) _and_ be an
rvalue for -(Â) whose destination type again is large enough to
hold the value of (b * 2u).

   long long a;
   unsigned long b;

   b = 8;
   a = (int)(-(b * 2U));

Why does this work when your example does not?

For once, it does not give exact sizes for the objects in use. Secondly, the type cast is acually redundant to the original version. Finally, the example may work for b = 8 but not for b = 0xffffffff and hence is _wrong_ with b being a _variable_ whose value no compiler can ever predict an therefore _should_ use a temporary with an appropriate type which, again, is the point of the whole discussion.


Cheers, Christian


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]