231. Power of Two

来源:互联网 发布:詹姆斯哈登数据 编辑:程序博客网 时间:2024/05/21 09:12

Given an integer, write a function to determine if it is a power of two.

class Solution {public:    bool isPowerOfTwo(int n) {        return (n>0)&&!(n&(n-1));    }};

首先需要n大于0,然后n和n-1进行与处理,如果n是2的指数,那么n&(n-1)返回的是0,也就是false,这时需要在前面加个非。

0 0