#include <iostream>
using namespace std;


bool TestPower(int n)
{
if (n == 1)
return false;
int count = 0;
do
{
if (n & 1 == 1)
{
++count;
if (count > 1)
return false;
}
} while ( (n >>= 1) > 0);

if (count == 1)
return true;
else
return false;
}


int main()
{
int a = 4;
int b = 1024;
int c = 1023;

if (TestPower(a))
cout<<"a is power of 2"<<endl;
else
cout<<"a is not power of 2"<<endl;

if (TestPower(b))
cout<<"b is power of 2"<<endl;
else
cout<<"b is not power of 2"<<endl;

if (TestPower(c))
cout<<"c is power of 2"<<endl;
else
cout<<"c is not power of 2"<<endl;

return 0;
}

相关文章:

  • 2021-09-13
  • 2021-11-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
猜你喜欢
  • 2022-01-30
  • 2021-04-19
  • 2022-12-23
  • 2021-06-18
  • 2022-12-23
  • 2022-12-23
  • 2021-12-18
相关资源
相似解决方案