我认为代码应该阅读(给出问题描述。)你想知道 N 是否是 2 的幂?
编辑为代码
#include <iostream>
int main()
{
unsigned int N;
std::cout << "Input a number ";
std::cin >> N;
unsigned int two_to_the_power_of_bit = 0;
do {
std::cout << "Working on " <<
two_to_the_power_of_bit << std::endl;
if (two_to_the_power_of_bit == N) {
std::cout << "TRUE" << std::endl;
return 0;
}
if (two_to_the_power_of_bit > N) {
std::cout << "FALSE" << std::endl;
return 1;
}
if (two_to_the_power_of_bit == 0) {
two_to_the_power_of_bit = 1;
} else {
two_to_the_power_of_bit <<= 1;
}
} while(two_to_the_power_of_bit);
}
如果我把你的问题弄错了,你能澄清一下吗?
Output:
Input a number 3
Working on 0
Working on 1
Working on 2
Working on 4
FALSE
mehoggan@mehoggan-Precision-WorkStation-T5500:~/Devel/test$ ./a.out
Input a number 4
Working on 0
Working on 1
Working on 2
Working on 4
TRUE
mehoggan@mehoggan-Precision-WorkStation-T5500:~/Devel/test$ ./a.out 5
Input a number 5
Working on 0
Working on 1
Working on 2
Working on 4
Working on 8
FALSE
mehoggan@mehoggan-Precision-WorkStation-T5500:~/Devel/test$