Given an integer, write a function to determine if it is a power of two.
2的幂数二进制只有一个1
1 bool isPowerOfTwo(int n) { 2 if(n <= 0) 3 return 0; 4 return (n & (n-1)) == 0; 5 }
时间: 2024-10-05 04:59:53
Given an integer, write a function to determine if it is a power of two.
2的幂数二进制只有一个1
1 bool isPowerOfTwo(int n) { 2 if(n <= 0) 3 return 0; 4 return (n & (n-1)) == 0; 5 }