C问题:最经济的整数检验是2的幂 [英] C Question: Most Economical Test For Integer Is a Power of 2

查看:55
本文介绍了C问题:最经济的整数检验是2的幂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

'C'中最经济的测试是什么?整数是2的幂?


例如,更好的东西:


void is_2_pow(int arg)

{

return((x == 1)||(x == 2)||(x == 4)||(x == 8)||(x == 16)/ *和

等等* /);

}

What is the most economical test in ''C'' for "integer is a power of 2"?

For example, something better than:

void is_2_pow(int arg)
{
return((x == 1) || (x == 2) || (x == 4) || (x == 8) || (x == 16) /* and
so on */ );
}

推荐答案

" David T. Ashley" < dt*@e3ft.com写在留言中

新闻:B4 ***************************** *@giganews.com ...
"David T. Ashley" <dt*@e3ft.comwrote in message
news:B4******************************@giganews.com ...

C中最经济的测试整数是2的幂是什么?


例如,更好的东西:


void is_2_pow(int arg)

{

return((x == 1)||(x == 2)||(x == 4)||(x == 8)||(x == 16)/ *和

等等* /);

}
What is the most economical test in ''C'' for "integer is a power of 2"?

For example, something better than:

void is_2_pow(int arg)
{
return((x == 1) || (x == 2) || (x == 4) || (x == 8) || (x == 16) /* and
so on */ );
}



让我们再试一次:


void is_2_pow(int x)

{

return((x == 1)||(x == 2)||(x == 4) ||(x == 8)||(x == 16)/ *和

等等* /);

}


但我想每个人都会知道我的意思。

Let''s try that again:

void is_2_pow(int x)
{
return((x == 1) || (x == 2) || (x == 4) || (x == 8) || (x == 16) /* and
so on */ );
}

but I think everyone would have known what I meant.




David T. Ashley写道:

David T. Ashley wrote:

" David T. Ashley" < dt*@e3ft.com写在留言中

新闻:B4 ***************************** *@giganews.com ...
"David T. Ashley" <dt*@e3ft.comwrote in message
news:B4******************************@giganews.com ...

C中最经济的测试整数是2的幂是什么?


例如,更好的东西:


void is_2_pow(int arg)

{

return((x == 1)||(x == 2)||(x == 4)||(x == 8)||(x == 16)/ *和

等等* /);

}
What is the most economical test in ''C'' for "integer is a power of 2"?

For example, something better than:

void is_2_pow(int arg)
{
return((x == 1) || (x == 2) || (x == 4) || (x == 8) || (x == 16) /* and
so on */ );
}



让我们再试一次:


void is_2_pow(int x)

{

return((x == 1)||(x == 2)||(x == 4) ||(x == 8)||(x == 16)/ *和

等等* /);

}


Let''s try that again:

void is_2_pow(int x)
{
return((x == 1) || (x == 2) || (x == 4) || (x == 8) || (x == 16) /* and
so on */ );
}



你在void函数中返回一个值。应该是:int

is_2_pow(int x)


可能不是最经济的方法,但我会这样做:

int isPowerof2(int x){

unsigned int i;

int count = 0;

int n = 1;


for(i = INT_MAX; i; i>> = 1){

if(x& n)

count + +;

n<< = 1;

}

if(count 1)

count = 0 ;


返回计数;

}

You''re returning a value in a void function. Should be: int
is_2_pow(int x)

Probably not the most economical method, but I''d do:
int isPowerof2(int x) {
unsigned int i;
int count = 0;
int n = 1;

for (i = INT_MAX; i; i >>= 1) {
if (x & n)
count ++;
n <<= 1;
}
if (count 1)
count = 0;

return count;
}


David T. Ashley写道:
David T. Ashley wrote:

>

''C'中最经济的测试是什么?整数是2的幂?


例如,比以下更好的东西:


void is_2_pow(int arg)

{

return((x == 1)||(x == 2)||(x == 4)||(x == 8)||(x == 16)/ *和

依此类推* /);

}
>
What is the most economical test in ''C'' for "integer is a power of 2"?

For example, something better than:

void is_2_pow(int arg)
{
return((x == 1) || (x == 2) || (x == 4) || (x == 8) || (x == 16) /* and
so on */ );
}



int n_is_Power_of_two(长期无符号n)

{

return(n& n - 1)== 0&& n!= 0;

}


-

pete


int n_is_Power_of_two(long unsigned n)
{
return (n & n - 1) == 0 && n != 0;
}

--
pete


这篇关于C问题:最经济的整数检验是2的幂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆