C++ 快速查找2^x的方法
如何在C中快速找到2^x。如果你们有任何想法,请帮助。是int还是float?对于int,使用左移位。对于float,pow()函数回想一下,在二进制系统中,C++ 快速查找2^x的方法,c++,c,C++,C,如何在C中快速找到2^x。如果你们有任何想法,请帮助。是int还是float?对于int,使用左移位。对于float,pow()函数回想一下,在二进制系统中,N位置的一位表示2^N。因此,正int的公式为 1 << x 1Bitshift向左,每移位一位,数字就乘以2,就像向左移动十进制数字乘以10一样 使用#包含 浮动功率(浮动x,浮动y);/*C99*/ 双功率(双x,双y); 长双刀(长双刀x,长双刀y);/*C99*/ 在x第位位置设置1:1它被称为位移位。提示,用二进制2
N
位置的一位表示2^N
。因此,正int
的公式为
1 << x
1Bitshift向左,每移位一位,数字就乘以2,就像向左移动十进制数字乘以10一样
使用#包含
浮动功率(浮动x,浮动y);/*C99*/
双功率(双x,双y);
长双刀(长双刀x,长双刀y);/*C99*/
在x
第位位置设置1
:1它被称为位移位。提示,用二进制2^n
表示几个小的n值,您将得到算法…”
int twoPowZero = 1; // any number^0 is 1
int twoPowOne = 1 << 1; // this sets the '2' bit to '1'
int twoPowTwo = 1 << 2;
int twoPowFive = 1 << 5;
int twoPowTen = 1 << 10;
#include <stdio.h>
#include <math.h>
int main ()
{
printf ("7.0 ^ 3 = %lf\n", pow (7.0,3));
printf ("4.73 ^ 12 = %lf\n", pow (4.73,12));
printf ("32.01 ^ 1.54 = %lf\n", pow (32.01,1.54));
return 0;
}
7.0 ^ 3 = 343.000000
4.73 ^ 12 = 125410439.217423
32.01 ^ 1.54 = 208.036691
#include <math.h>
float powf(float x, float y); /* C99 */
double pow(double x, double y);
long double powl(long double x, long double y); /* C99 */