LeetCode:Power of Two Posted on 2016-05-20 In 编程 Disqus: Given an integer, write a function to determine if it is a power of two. 解题思路1,最高位为1,其余都为0.位运算判断方法为num&(num-1)2.循环 code1234567bool isPowerOfTwo(int n) { if(n <= 0) return false; while(n%2 == 0) { n /= 2; } return 1 == n;} 123456class Solution {public: bool isPowerOfTwo(int n) { return n > 0 && 0 == (n & (n-1)); }};