Re: Strange conversion to int64

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

 



To ensure you get what you'd expect, it's a good idea to explicitly
cast your operands in these mixed sign cases.

  Brian

On Wed, Jan 27, 2010 at 7:53 PM, me22 <me22.ca@xxxxxxxxx> wrote:
> 2010/1/27 Tony Bernardin <sarusama@xxxxxxxxx>:
>>
>> 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
>


[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