Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

how to find a number whether its a power of 2 or not

0 views
Skip to first unread message

MJ

unread,
Apr 27, 2005, 1:33:29 AM4/27/05
to
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...

Walter Roberson

unread,
Apr 27, 2005, 2:08:50 AM4/27/05
to
In article <1114580009.2...@g14g2000cwa.googlegroups.com>,

MJ <mayur...@gmail.com> wrote:
>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

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

Martijn

unread,
Apr 27, 2005, 2:15:10 AM4/27/05
to
> X = 2 power n
> I need to find it using a Single C statement whether its a power of 2
> or not

http://www.catch22.net/source/snippets.asp

Top of the page

--
Martijn
http://www.sereneconcepts.nl


Kenny McCormack

unread,
Apr 27, 2005, 8:41:45 AM4/27/05
to
>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

The answer is "Yes".

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

Richard Bos

unread,
Apr 27, 2005, 11:17:45 AM4/27/05
to
"MJ" <mayur...@gmail.com> wrote:

> I need to find it using a Single C statement whether its a power of 2
> or not

If you need to use a single statement, it's not a real problem, it's
homework. So consider _why_ you've been given this homework... would
that be to learn something about copying code from a newsgroup? I think
not.

Richard

Gregory Pietsch

unread,
Apr 27, 2005, 11:23:33 AM4/27/05
to
unsigned int is_power_of_2(unsigned int x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}

/* Gregory Pietsch */

pete

unread,
Apr 28, 2005, 12:40:16 PM4/28/05
to


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

int n_is_Power_of_four(unsigned n)
{
return !(n & n - 1) && n % 3 == 1;
}

int n_is_Power_of_eight(unsigned n)
{
return !(n & n - 1) && n % 7 == 1;
}

--
pete

Peter Nilsson

unread,
Apr 28, 2005, 9:38:06 PM4/28/05
to
pete wrote:
> int n_is_Power_of_two(unsigned n)
> {
> return !(n & n - 1) && n % 1 == 1;
> }

For what unsigned n is n % 1 == 1? ;)

--
Peter

pete

unread,
Apr 29, 2005, 2:39:28 AM4/29/05
to

It's part of the sequence:


return !(n & n - 1) && n % 1 == 1

return !(n & n - 1) && n % 3 == 1

pete

unread,
Apr 29, 2005, 2:50:09 AM4/29/05
to

However, it seems to be completely wrong. Thank you.

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

--
pete

Jonathan Adams

unread,
Apr 29, 2005, 6:01:13 AM4/29/05
to
In article <4271D9...@mindspring.com>,
pete <pfi...@mindspring.com> wrote:

Is 0 a power of two, then?

- jonathan

pete

unread,
Apr 29, 2005, 6:44:22 PM4/29/05
to

Not really. Thank you.

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

--
pete

0 new messages