C语言 检查正整数 N 的 2 的幂和大 N 的错误



我有这个程序来检查 no 是否为 2 的幂,但如果no = 1099511627776,它不适用于测试用例:

int not; // no of testcase
unsigned long long int no; // input number
int arr[100];
scanf ("%d", &not);
for ( int i = 0; i < not; i++ )
{
scanf ("%llu", &no);
if ( no & !(no & (no - 1))) // check wheather no is power of 2 or not excludig 0
arr[ i ] = 1;
else
arr[ i ] = 0;
}
for ( int i = 0; i < not; i++ )
{
if ( arr[ i ] == 1 )
printf ("YESn");
else
printf ("NOn");
}

您会收到错误,因为no为零的比较不是与运算符一起使用&良好的逻辑表达式。

在单独使用值的情况下,您可以侥幸逃脱,但由于no是 2 的幂,0在最低有效位中,因此no & [some-logical-expression]产生零。

您可以通过三种方式解决此问题:

  • 使用&&代替&,即no && !(no & (no - 1))
  • no前面添加!!,即!!no & !(no & (no - 1))
  • 将显式比较添加到零,即no!=0 & !(no & (no - 1)).

我非常喜欢第一种方法。

演示。

&是按位 AND 的。你想要逻辑和:no && !(no & (no - 1))

这是我从你提到的代码中观察到的一些意见。

首先,如果用户给出的价值超过100not怎么办?如果按照您声明arr大小not>100,则会导致未定义的行为100.为了避免这种情况,一种方法是首先扫描not,然后创建等于not大小的数组。例如

int not = 0; // no of testcase
scanf ("%d", &not);
int arr[not]; /* create array equal to not size.. */

或者动态创建数组,例如

int not = 0; // no of testcase
scanf ("%d", &not);
int *arr = malloc(not * sizeof(*arr)); /* create dynamic array equal to not size.. */

其次,要检查给定的数字是否为 2 的幂,这个!(no & (no - 1))是正确的,但要排除zero即如果给定的输入no0那么你不应该检查这个!(no & (no - 1))。 为此,请使用逻辑 AND&&运算符。这

if ( no & !(no & (no - 1))) { 
}

应该是

if ( no && !(no & (no - 1))) {
} 

最新更新