【Leetcode】2的幂(整数的二进制形式,与运算)

 

 

 

class Solution {
public:
    bool isPowerOfTwo(int n) {
        if(n <= 0) return false;
        
        return (n&(n-1)) == 0;
    }
};

注:

1) 2的幂函数,其y值大于0;

2) 2的幂函数,若 x < 0, y = (0,1);

2)n&(n-1) == 0 和 (n&(n-1)) == 0 ,逻辑是不一样的。

 

相关文章:

  • 2022-12-23
  • 2021-12-23
  • 2021-06-29
  • 2021-06-27
  • 2021-06-21
  • 2021-11-30
  • 2022-01-20
  • 2021-09-07
猜你喜欢
  • 2021-11-25
  • 2022-12-23
  • 2021-11-10
  • 2021-12-10
  • 2022-12-23
  • 2022-01-13
  • 2021-11-29
相关资源
相似解决方案