Java Bitshift替换Math.pow
我需要用bitshift替换Java中的Math.powJava Bitshift替换Math.pow,java,math,pow,Java,Math,Pow,我需要用bitshift替换Java中的Math.pow for (int i = n - 1; i >= 0; i--) Math.pow(16, n - i - 1) 其中n是十六进制数的长度。 13304fb表示n=7。 基本上就是把十六进制转换成十进制 现在我需要用Bitshift替换Math.pow。我想不出来,因为n可以是它想要的那么大 16^(n-i-1)=2^(4*(n-i-1)) 2^x=1更通用的方式: public static double posIntP
for (int i = n - 1; i >= 0; i--)
Math.pow(16, n - i - 1)
其中n是十六进制数的长度。13304fb表示n=7。
基本上就是把十六进制转换成十进制 现在我需要用Bitshift替换Math.pow。我想不出来,因为n可以是它想要的那么大
16^(n-i-1)
=2^(4*(n-i-1))
2^x
=1更通用的方式:
public static double posIntPow(final double pVal, final int pPow) {
double ret = 1;
double v1, v2;
int n = pPow;
v1 = pVal;
if ((n & 1) == 1) {
ret = pVal;
}
n = n >>> 1;
while (n > 0) {
v2 = v1 * v1;
if ((n & 1) == 1) {
ret = ret * v2;
}
v1 = v2;
n = n >>> 1;
}
return ret;
}
啊!!我试着把它看成16^x=1