请问我要打2的6次方如何打,我打2^6为何结果是4

解决方案 »

  1.   

    double i=java.lang.Math.pow(2,6);
      

  2.   


    //1
    int result = 1;
    for(int i = 0;i<6;++i)
    {
    result *= 2;
    }
    System.out.println(result);
    //2
    System.out.println(Math.pow(2,6));
    //3
    System.out.println(2<<5);
      

  3.   

    double i=java.lang.Math.pow(2,6);
      

  4.   

    2^6 表示按二进制异或2:010
    6:110
    ------
    4:100幂计算没有现成的运算符,需要采用 Math.pow 方法,或者自己实现一个: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);
        }    private 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;
        }
    }
      

  5.   

    用移位只是针对这个2为底的特列,不值得提倡。a^b有现成的API用,想知道它的实现,看火龙果的代码,
    大家别小看这段代码啊,普通的求pow就像一任天然§不务正业那样只能获得O(n)的复杂度

        private 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;
        }
    可是O(lgn)啊,呵呵,相当不错,值得学习哦,分治算法。
      

  6.   

    2 >> 5 ,,pow(2,6)。
    对于时间复杂度还不是很懂。
      

  7.   

    Math.pow(2 , 6)和2<<5都没有问题,不过2<<5处理起来速度更快些
      

  8.   

    System.out.println(Math.pow(2,6));