如何找到一个数字是否为2的幂 [英] how to find a number whether its a power of 2 or not

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

问题描述



我对C有疑问。

我有一个数字X是2的幂

X = 2次幂n

我需要使用单个C语句找到它,无论它的功率是2

还是没有

我知道这个数字的功率是2将只有一个单位为1

,其余位为0.要检查位,我需要通过所有

位并检查条件。我不想使用循环。在那个

的情况下我该怎么做呢


提前谢谢

问候

Mayur。 ..

Hi
I have question about the C.
I have a number X which is power of two
X = 2 power n
I need to find it using a Single C statement whether its a power of 2
or not
I know that a number with power of 2 will have only one single bit as 1
and rest of the bit as 0. To check the bit I need to go through all the
bits ones and check for the condition. I dont want use a loop. In that
case how can I do this

Thanks in advance
Regards
Mayur...

推荐答案

文章< 11 ****************** ****@g14g2000cwa.googlegroups .com> ;,

MJ< ma ******** @ gmail.com>写道:
In article <11**********************@g14g2000cwa.googlegroups .com>,
MJ <ma********@gmail.com> wrote:
我对C有疑问。
我有一个数字X,这是两个的力量
X = 2的力量n
我需要找到它一个单一的C语句是否是2的力量
I have question about the C.
I have a number X which is power of two
X = 2 power n
I need to find it using a Single C statement whether its a power of 2
or not




这个问题好像每隔几周被问到一次。

考虑一下:如果X是2的幂,那么(X-1)在所有位位置(除了前导0'之外)与X

不同。< br $> b $ b -

''Walter所说的简短版本是你问了一个问题

没有用的答案,请重新考虑您希望解决的问题的性质。 - Tony Mantler



This question seems to get asked every couple of weeks.

Consider this: if X is a power of 2, then (X-1) differs from X
in all the bit positions (other than the leading 0''s).
--
''The short version of what Walter said is "You have asked a question
which has no useful answer, please reconsider the nature of the
problem you wish to solve".'' -- Tony Mantler


> X = 2次幂n
> X = 2 power n
我需要使用单个C语句找到它,无论它的功率是否为2
I need to find it using a Single C statement whether its a power of 2
or not



=nofollowhref =http://www.catch22.net/source/snippets.asptarget =_ blank> http://www.catch22.net/source/snippets.asp


页面顶部


-

Martijn
http://www.sereneconcepts.nl


文章< 11 ***********************g14g2000cwa.googlegroups .com> ;,

MJ< ma ******** @ gmail.com>写道:
In article <11**********************@g14g2000cwa.googlegroups .com>,
MJ <ma********@gmail.com> wrote:

我对C有疑问。
我有一个数字X,这是两个的力量
X = 2的力量n
我需要使用Single C语句找到它,无论它的功率是否为2
Hi
I have question about the C.
I have a number X which is power of two
X = 2 power n
I need to find it using a Single C statement whether its a power of 2
or not




答案是是。


(在您的问题陈述中,您声明X是2的幂)



The answer is "Yes".

(In your problem statement, you state that X is a power of two)


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

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