Given an integer,write a function to determine if it is a power of two.
要求答题者判断一个整型是否是2的幂次方。
设整数n为2的幂次方,n的二进制表示为01000.....,
n-1的二进制表示为00111...
有且仅当n为2的幂次方时n&(n-1)时才为0
=>
bool isPowerOfTwo(int n) {
return (n>0)?!(n&(n-1)):0;
}
Given an integer,write a function to determine if it is a power of two.
要求答题者判断一个整型是否是2的幂次方。
设整数n为2的幂次方,n的二进制表示为01000.....,
n-1的二进制表示为00111...
有且仅当n为2的幂次方时n&(n-1)时才为0
=>
bool isPowerOfTwo(int n) {
return (n>0)?!(n&(n-1)):0;
}