#include<stdio.h>
#define ERROR -1
#define SUCESS 1
#define FALURE 0
int checkpow2(int num);
int main()
{
int num;
int result;
printf("PLS input your test num:\n");
scanf("%d",&num);
result=checkpow2(num);
if(result)
printf("your number is pow2!\n");
else
printf("your number is not pow2\n");
}
int checkpow2(int num)
{
if(num<=0)
return ERROR;
else if(0==(num&(num-1)))
return SUCESS;
else
return FALURE;
}
#define ERROR -1
#define SUCESS 1
#define FALURE 0
int checkpow2(int num);
int main()
{
int num;
int result;
printf("PLS input your test num:\n");
scanf("%d",&num);
result=checkpow2(num);
if(result)
printf("your number is pow2!\n");
else
printf("your number is not pow2\n");
}
int checkpow2(int num)
{
if(num<=0)
return ERROR;
else if(0==(num&(num-1)))
return SUCESS;
else
return FALURE;
}
不就是除了最高位,其它位都是0吗。
这个在操作大数的时候效率明显提高。
linux下经验证通过!
本文介绍了一个利用位运算快速判断整数是否为2的幂次方的方法,并提供了相应的C语言代码实现,适用于操作大数场景。此方法在Linux环境下验证通过。
1万+

被折叠的 条评论
为什么被折叠?



