231 Power of Two 2的幂
给定一个整数,写一个函数来判断它是否是2的幂。
详见:https://leetcode.com/problems/power-of-two/description/
Java实现:
class Solution { public boolean isPowerOfTwo(int n) { return (n>0)&&((n&(n-1))==0); } }
C++实现:
class Solution { public: bool isPowerOfTwo(int n) { return (n>0)&&(!(n&(n-1))); } };