Multiplication of two integers in C++

后端 未结 3 2008
深忆病人
深忆病人 2020-11-30 13:56

I have a pretty basic question, but I am not sure if I understand the concept or not. Suppose we have:

int a = 1000000;
int b = 1000000;
long long c = a * b;         


        
3条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-30 14:06

    It's kind of absurd, because the assembler instruction does always compute

    int * int -> 64 bits long

    so if you look at the machine code, you see : imul that store 64bits into eax edx then cdq that put the bit sign of eax into edx (thus losing the full 64bits result) and then eax edx are stored into the 64bits variable

    and if you convert the 32bits values into 64bits before the multiplication, you get a call to the 64bits multiplication function for no reason

    (I checked : it's not the case when the code is optimized)

提交回复
热议问题