This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Strange conversion to int64
2010/1/27 Tony Bernardin <sarusama@gmail.com>:
>
> I was expecting to get -64 in every case. I don't understand why
> (int64) = -1 (int32) * 64 (uint32)
> is different from
> (int64) = -1 (int32) * 64 (uint64)
>
> Also why is
> (int64) = -1 (int32) * 64 (uint32)
> is different from
> (int32) = -1 (int32) * 64 (uint32)
>
The difference is in whether the multiplicand or the product is being
promoted, and thus whether things are being sign- or zero-extended.
Here's why (and how) you get 4294967232:
(int32)-1 * (uint32)64
Then promote both multiplicands to unsigned
(uint32)4294967295 * (uint32)64
Perform the unsigned 32-bit multiplication
(uint32)4294967232
Promote to 64-bits, using zero-extension since it's unsigned
(uint64)4294967232
Then coerce it to signed
(int64)4294967232
HTH,
~ Scott