标签:blog ar io os sp 2014 log 代码 amp
思路:一个整数如果是2的整数次方,那么它的二进制表示中有且只有一位是1,而其他所有位都是0。把这个整数与这个整数减去1之后进行与运算,那么这个整数当中唯一的
1会变为0,这个整数也变为0;
代码:
// Is2.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include <iostream>
using namespace std;
bool is2(int n)
{
return !( n&(n-1));
}
int _tmain(int argc, _TCHAR* argv[])
{
bool b = is2(2);
cout<<b<<endl;
bool c = is2(3);
cout<<c<<endl;
getchar();
return 0;
}标签:blog ar io os sp 2014 log 代码 amp
原文地址:http://blog.csdn.net/djb100316878/article/details/42001341