if(x&&!(x&(x-1))== 0) [英] if (x && !(x & (x-1)) == 0)

查看:86
本文介绍了if(x&&!(x&(x-1))== 0)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

原始问题:

"给出一个单行C表达式来测试一个数字是否为
是2的幂。[不允许循环 - 它''一个简单的测试。]


答案:

if(x&&!(x&(x-1))== 0 )


我的问题:

为什么这个表达式有效?


谢谢。

Original question:
"Give a one-line C expression to test whether a number
is a power of 2. [No loops allowed - it''s a simple test.]"

Answer:
if (x && !(x & (x-1)) == 0)

My question:
Why does this expression work?

Thanks.

推荐答案

" William" < WH ****** @ student.cs.uwaterloo.ca>在消息中写道

新闻:Pi ****************************** @ cpu02.studen t。 cs.uwaterloo.ca ...
"William" <wh******@student.cs.uwaterloo.ca> wrote in message
news:Pi******************************@cpu02.studen t.cs.uwaterloo.ca...
原始问题:
给出一个单行C表达式来测试一个数字是否是2的幂。[无循环允许 - 这是一个简单的测试。]

答案:
if(x&&!(x&(x-1))== 0)

我的问题:
为什么这个表达式有用?
Original question:
"Give a one-line C expression to test whether a number
is a power of 2. [No loops allowed - it''s a simple test.]"

Answer:
if (x && !(x & (x-1)) == 0)

My question:
Why does this expression work?




错误的组。

无论如何都有提示:想到权力的二元表示而不是权力

of 2.你会看到答案。我就此止步。


Alex



Wrong group.
A hint anyway: think of binary representation of powers of 2 and not powers
of 2. You''ll see the answer. I stop here.

Alex


" Alexei A. Frounze" <人***** @ chat.ru>写道:
"Alexei A. Frounze" <al*****@chat.ru> writes:
" William" < WH ****** @ student.cs.uwaterloo.ca>在消息中写道
新闻:Pi ****************************** @ cpu02.studen t.cs.uwaterloo。 ca ...
"William" <wh******@student.cs.uwaterloo.ca> wrote in message
news:Pi******************************@cpu02.studen t.cs.uwaterloo.ca...
原始问题:
给出一个单行C表达式来测试一个数字是否是2的幂。[不允许循环 - 它' 一个简单的测试。

答案:
如果(x&&!(x&(x-1))== 0)

我的问题:
为什么这个表达有效?
错误的小组。
Original question:
"Give a one-line C expression to test whether a number
is a power of 2. [No loops allowed - it''s a simple test.]"

Answer:
if (x && !(x & (x-1)) == 0)

My question:
Why does this expression work?
Wrong group.




这是怎么回事?

无论如何都要提示:想想2的权力的二元表示而不是2的权力。你会看到答案。我就此止步。



How is this the wrong group?
A hint anyway: think of binary representation of powers of 2 and not powers
of 2. You''ll see the answer. I stop here.




很难理解你的意思。我想*你的意思是


想想

二元表示2的功率

而不是

的权力2.


但是当我第一次看到它时,我以为你在谈论的是权力为2

而不是2的权力,没有任何意义。


-

Keith Thompson(The_Other_Keith) ks *** @ mib.org < http://www.ghoti.net/~kst>

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

我们必须做点什么。这是事情。因此,我们必须这样做。



It''s hard to understand what you mean by that. I *think* you mean

Think of
binary representation of powers of 2
and not
powers of 2.

but when I first read it I thought you were talking about "powers of 2
and not powers of 2", which doesn''t make any sense.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.


让x = 1000(8,幂为2); x-1 = 111

然后

x& (x-1)= 1111

!(x&(x-1))= 0000

怎么办(1000&& 0000)== 0?


2005年8月14日,Sun,Keith Thompson写道:
let x = 1000 (8, a power of 2); x-1 = 111
then
x & (x-1) = 1111
!(x & (x-1)) = 0000
how does (1000 && 0000) == 0?


On Sun, 14 Aug 2005, Keith Thompson wrote:
" Alexei A. Frounze" <人***** @ chat.ru>写道:
"Alexei A. Frounze" <al*****@chat.ru> writes:
" William" < WH ****** @ student.cs.uwaterloo.ca>在消息中写道
新闻:Pi ****************************** @ cpu02.studen t.cs.uwaterloo。 ca ...
"William" <wh******@student.cs.uwaterloo.ca> wrote in message
news:Pi******************************@cpu02.studen t.cs.uwaterloo.ca...
原始问题:
给出一个单行C表达式来测试一个数字是否是2的幂。[不允许循环 - 它' 一个简单的测试。

答案:
如果(x&&!(x&(x-1))== 0)
为什么这个表达有效?
Original question:
"Give a one-line C expression to test whether a number
is a power of 2. [No loops allowed - it''s a simple test.]"

Answer:
if (x && !(x & (x-1)) == 0)

My question:
Why does this expression work?



错误的组。



Wrong group.



这是怎么回事?<无论如何,



How is this the wrong group?

提示:想想2的幂的二进制表示而不是2的权力。你会看到答案。我就此止步。
A hint anyway: think of binary representation of powers of 2 and not powers
of 2. You''ll see the answer. I stop here.



很难理解你的意思。我认为*你的意思是

想想
二进制代表2的权力而不是

但是当我第一次看到它我认为你在谈论的是2的力量而不是2的力量,这没有任何意义。

-
基思汤普森(The_Other_Keith) ks***@mib.org < http://www.ghoti.net/~kst> 我们必须做点什么。这是事情。因此,我们必须这样做。



It''s hard to understand what you mean by that. I *think* you mean

Think of
binary representation of powers of 2
and not
powers of 2.

but when I first read it I thought you were talking about "powers of 2
and not powers of 2", which doesn''t make any sense.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.



这篇关于if(x&amp;&amp;!(x&amp;(x-1))== 0)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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