Strange conversion to int64

me22 me22.ca@gmail.com
Thu Jan 28 03:53:00 GMT 2010


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



More information about the Gcc-help mailing list