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

M

MJ

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...
 
W

Walter Roberson

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).
 
K

Kenny McCormack

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)
 
R

Richard Bos

MJ said:
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
 
G

Gregory Pietsch

unsigned int is_power_of_2(unsigned int x)
{
return (x != 0) && ((x & (x - 1)) == 0);
}

/* Gregory Pietsch */
 
P

pete

MJ said:
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


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;
}
 
P

pete

Peter said:
For what unsigned n is n % 1 == 1? ;)

It's part of the sequence:
return !(n & n - 1) && n % 1 == 1
return !(n & n - 1) && n % 3 == 1
return !(n & n - 1) && n % 7 == 1
 
P

pete

pete said:
It's part of the sequence:
return !(n & n - 1) && n % 1 == 1
return !(n & n - 1) && n % 3 == 1
return !(n & n - 1) && n % 7 == 1

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

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

Jonathan Adams

pete said:
However, it seems to be completely wrong. Thank you.

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

Is 0 a power of two, then?

- jonathan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,902
Latest member
Elena68X5

Latest Threads

Top