测试一个数是否是2的幂 [英] test whether a number is a power of 2

查看:63
本文介绍了测试一个数是否是2的幂的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出一个单行C表达式来测试一个数字是否是2的幂。

不允许循环。

Give a one-line C expression to test whether a number is a power of 2.
No loops allowed.

推荐答案

Matt< jr ******** @ hotmail.com>潦草地写道:
Matt <jr********@hotmail.com> scribbled the following:
给出一个单行C表达式来测试一个数字是否是2的幂。
不允许循环。
Give a one-line C expression to test whether a number is a power of 2.
No loops allowed.




((((x&&!(x&(x - !! x)))+ x)-x)^ x)^ x


做什么我赢了吗?


-

/ - Joona Palaste(pa*****@cc.helsinki.fi)------ --------------------- \

| 飞翔的柠檬树中的金鸡王G ++ FR FW + M-#108 D + ADA N +++ |

| http://www.helsinki.fi/~palaste W ++ B OP + |

\ -----------------------------------------芬兰的规则! ------------ /

记住:只有三种人 - 那些可以计算的人和那些可以'b
'的人't。"

- Vampyra



((((x&&!(x&(x-!!x)))+x)-x)^x)^x

What do I win?

--
/-- Joona Palaste (pa*****@cc.helsinki.fi) ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Remember: There are only three kinds of people - those who can count and those
who can''t."
- Vampyra


" Matt" < JR ******** @ hotmail.com>在消息中写道

新闻:ba ************************** @ posting.google.c om ...
"Matt" <jr********@hotmail.com> wrote in message
news:ba**************************@posting.google.c om...
给出一个单行C表达式来测试一个数字是否为2的幂。
不允许循环。
Give a one-line C expression to test whether a number is a power of 2.
No loops allowed.




isPow2 = x&& !((x-1)& x);


参见 http://www.caam.rice.edu/~dougm/twiddle/

获取相关信息。

hth

-
http:// ivan.vecerina.com
http://www.brainbench.com <> Brainbench MVP for C ++



isPow2 = x && !( (x-1) & x );

See http://www.caam.rice.edu/~dougm/twiddle/
for some related info.

hth
--
http://ivan.vecerina.com
http://www.brainbench.com <> Brainbench MVP for C++



jr ********@hotmail.com (Matt)写道:
给出一个单行C表达式来测试一个数字是否为2的幂。
不允许循环。
Give a one-line C expression to test whether a number is a power of 2.
No loops allowed.




我们不是来为你做功课。


-

Keith Thompson(The_Other_Keith) ks*@cts.com < http://www.ghoti.net/~kst> ;

圣地亚哥超级计算机中心< *> < http://www.sdsc.edu/~kst>

Schroedinger做莎士比亚:要*和*不要



We''re not here to do your homework for you.

--
Keith Thompson (The_Other_Keith) ks*@cts.com <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"


这篇关于测试一个数是否是2的幂的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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