are 2^n exponent calculations really less efficient than bit-shifts?

后端 未结 4 959
长情又很酷
长情又很酷 2020-12-15 21:01

if I do:

int x = 4;
pow(2, x);

Is that really that much less efficient than just doing:

1 << 4

?

4条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-15 21:56

    Generally yes, as bit shift is very basic operation for the processor.

    On the other hand many compilers optimise code so that raising to power is in fact just a bit shifting.

提交回复
热议问题