public static void main(String[] args) { // TODO Auto-generated method stub BigInteger num = new BigInteger(x); System.out.println(num.shiftLeft(64)); } 把上方的X换为你需要的数字就可以了~
首先 一个数的64次幂是很大的,int、long、double都不行,要用BigInteger类 BigInteger p = new BigInteger(x) ; for (int i=0; i<6; i++) p = p*p ; 位移不行 位移只是对位移的数进行乘二或除二
6 步就能算了,呵呵:1: x *= x 2: x *= x 3: x *= x 4: x *= x 5: x *= x 6: x *= x此时的 x 即为结果附:算一个数的 N 次方的算法,算法效率为 O(lgN)public class Test { public static void main(String[] args) { double n1 = pow(2, 6); double n2 = pow(2, -6); System.out.println(n1); System.out.println(n2); } public static double pow(double base, int exponent) { if (exponent < 0) return 1 / pow(base, -exponent); double power = 1; while(exponent > 0) { if((exponent & 1) == 1) { power *= base; } base *= base; exponent >>= 1; } return power; } }
// TODO Auto-generated method stub
BigInteger num = new BigInteger(x);
System.out.println(num.shiftLeft(64));
}
把上方的X换为你需要的数字就可以了~
x=?;//这里把?换成需要的数字
for(int i=0;i<6;++i) {
x*=x;
}当然楼上的位运算的效率要更高
1. p1 = x*x = x^2
2. p2 = p1 * p1 = x^4
3. p3 = p2 * p2 = x^8
4. p4 = p3 * p3 = x^16
5. p5 = p4 * p4 = x^32
6. p6 = p5 * p5 = x^64算法:double p = x ;
for (int i=0; i<6; i++) p = p*p ;
2. p2 = p1 * p1 = x^4
3. p3 = p2 * p2 = x^8
4. p4 = p3 * p3 = x^16
5. p5 = p4 * p3 = x^24
6. p6 = p5 * p4 = x^40
7. p7 = p6 * p4 = x^56
8. p8 = p7 * p3 = x^64
学习要举一反三 哈哈
BigInteger p = new BigInteger(x) ;
for (int i=0; i<6; i++) p = p*p ;
位移不行 位移只是对位移的数进行乘二或除二
2: x *= x
3: x *= x
4: x *= x
5: x *= x
6: x *= x此时的 x 即为结果附:算一个数的 N 次方的算法,算法效率为 O(lgN)public class Test {
public static void main(String[] args) {
double n1 = pow(2, 6);
double n2 = pow(2, -6);
System.out.println(n1);
System.out.println(n2);
} public static double pow(double base, int exponent) {
if (exponent < 0)
return 1 / pow(base, -exponent);
double power = 1;
while(exponent > 0) {
if((exponent & 1) == 1) {
power *= base;
}
base *= base;
exponent >>= 1;
}
return power;
}
}