Given an integer, write a function to determine if it is a power of two.
输入一个数,判断其是否为2的幂。
思路:可以按照326题的思路,用换底公式计算。也可以根据2进制的特点,2的幂一定是最高位是1,其他位是0。因此可以使用461题的技巧:n&(n-1)来计算1的个数。
这里使用另一个方法:利用STL的bitset模板直接求解。
关于bitset的参考:http://www.cppblog.com/ylfeng/archive/2010/03/26/110592.html
bool isPowerOfTwo(int n) {
bitset<100> a(n);
return a.count()==1;
}