Given an integer, write a function to determine if it is a power of two.
看一个数是不是2的幂,代码如下:
1 class Solution { 2 public: 3 bool isPowerOfTwo(int n) { 4 if(n<=0) return false; 5 bool isPower = false; 6 for(int i = 1; i < n; i<<=1){ 7 if(!isPower && (i&n)) 8 isPower = true; 9 else if(isPower && (i&n)){ 10 return false; 11 } 12 } 13 return true; 14 } 15 };
时间: 2024-10-12 03:24:25