#include<iostream>
using namespace std;
template <class T,class Integer>
T power(T x,Integer n)
{
if(n == 0) return 1;
if(n == 1) return x;
while((n&1) == 0)
{
x = x*x;
n >>= 1;
}
T result = x;
n >>= 1;
while(n != 0)
{
x = x*x;
if((n&1) != 0)
result *= x;
n >>= 1;
}
return result;
}
int main()
{
int x = 2;
int n = 30;
cout<<power<int,int>(x,n)<<endl;
return 0;
}原文:http://blog.csdn.net/u011487593/article/details/45533715