Re: Integral conversions in C/C++

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

 




On Sat 19/04/08  9:57 PM , Christian Böhme monodhs@xxxxxx sent:
> Hi all,
> 
> 
> 
> I ran into a problem with an expression that I imagined is too
> 
> trivial to even needing a second thought in the first place.
> 
> However, on a 32 bit machine, the code
> 
> 
> 
> --->8---
> 
> 
> 
> #include 
> 
> 
> 
> // ...
> 
> 
> 
> int64_t a;
> 
> uint32_t b = 8;
> 
> 
> 
> a = -(b * 2u);
> 
> 
> 
> --->8---
> 
> 
> 
> assigns a the value 0xfffffff0 while no warnings were issued even
> 
> when compiling with all warnings (-Wall) switched on.  Strangely
> 
> enough and for reasons escaping me

What type is the expression -(b * 2u)?  And why would the number 2^32 - 16 be
sign extended when stored in a 64-bit signed int?  2^32 - 16 is a valid positive
number for the 64-bit type.

Consider

int main(void)
{
   long long a;
   unsigned long b;

   b = 8;
   a = (int)(-(b * 2U));
   printf("a == %lld\n", a);
   return 0;
}

Why does this work when your example does not?

Tom



[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