题解:判断一个正整数是否是2^n(位运算方法)
1.题目
判断一个正整数是否是2^n
2.位运算法
思路:干掉二进制最右边的1,看是否是0
int main() { int num = 16; if ((num & (num - 1)) == 0) printf("the num is a 2^n"); else printf("the num is not a 2^n"); return 0; }
EOF
题解:判断一个正整数是否是2^n(位运算方法)
判断一个正整数是否是2^n
思路:干掉二进制最右边的1,看是否是0
int main() { int num = 16; if ((num & (num - 1)) == 0) printf("the num is a 2^n"); else printf("the num is not a 2^n"); return 0; }
EOF