A C Question.

J

Jack Young

Hi:
There is question about C programming:
Use just one C statement to check whether a variable is the power of 2. No
loops allowed.
Waiting for your answer.I have not slept well a few days for this.
 
E

Eric Sosman

Jack said:
Hi:
There is question about C programming:
Use just one C statement to check whether a variable is the power of 2. No
loops allowed.
Waiting for your answer.I have not slept well a few days for this.

int is_power_of_two(double x) {
return x > 0.0; /* one statement, no loops */
}

Sweet dreams!
 
R

Robert Gamble

Jack said:
Hi:
There is question about C programming:
Use just one C statement to check whether a variable is the power of 2. No
loops allowed.
Waiting for your answer.I have not slept well a few days for this.

puts(x & (x-1) || x==0 ? "not a power of two" : "power of two");

Robert Gamble
 
L

Logan Shaw

int is_power_of_two(double x) {
return x > 0.0; /* one statement, no loops */
}

Floating point numbers (at least IEEE and similar ones) can be expressed
as a fraction whose denominator is a power of two. But that is not the
same thing as the number itself being a power of two. For instance,
3.0 is not a power of two.

- Logan
 
K

Keith Thompson

Logan Shaw said:
Floating point numbers (at least IEEE and similar ones) can be expressed
as a fraction whose denominator is a power of two. But that is not the
same thing as the number itself being a power of two. For instance,
3.0 is not a power of two.

Sure it is; the exponent is approximately 1.58496.

The OP is expecting us to do his homework for him without even
providing a precise statement of the problem.
 
P

pete

Jack said:
Hi:
There is question about C programming:
Use just one C statement to check whether
a variable is the power of 2.
No loops allowed.
Waiting for your answer.I have not slept well a few days for this.

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

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

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

jat

modf(log(a)/log(2),&b)?0:1;

i have tried this on C++. I m not sure whether the functions are
available in C.
but the logic is that if we get a integer after taking the log of given
number with respect to 2(or any number), it is divisible.

n power x = y
x log(n) = log(y)
x = log(y)/log(n)

it may seem abstruse , but it's just another solution.
 
I

Ian Collins

jat said:
modf(log(a)/log(2),&b)?0:1;

i have tried this on C++. I m not sure whether the functions are
available in C.
but the logic is that if we get a integer after taking the log of given
number with respect to 2(or any number), it is divisible.

n power x = y
x log(n) = log(y)
x = log(y)/log(n)

it may seem abstruse , but it's just another solution.
Imagine waiting for this to run on an 8 bit microcontroller!
 
K

Keith Thompson

jat said:
modf(log(a)/log(2),&b)?0:1;

i have tried this on C++. I m not sure whether the functions are
available in C.
but the logic is that if we get a integer after taking the log of given
number with respect to 2(or any number), it is divisible.

n power x = y
x log(n) = log(y)
x = log(y)/log(n)

it may seem abstruse , but it's just another solution.

Another solution to what? The question was about determing whether a
number is a power of two, but we can't necessarily see the article to
which you're replying. Please read <http://cfaj.freeshell.org/google/>.

The method may be mathematically valid, but since floating-point
numbers are inexact, it's likely to fail in many cases.

But since the original poster was probably asking us to do his
homework for him, giving him *correct* answers is a bad idea.
 
J

Jordan Abel

Floating point numbers (at least IEEE and similar ones) can be expressed
as a fraction whose denominator is a power of two. But that is not the
same thing as the number itself being a power of two. For instance,
3.0 is not a power of two.

Sure it is.

2^1.58496250072115618146

oh, you meant an integer power of two?
 
N

Nelu

jmazzi said:
if ( var % 2 == 0 ) {
printf("yep\n");
}
Since when is 6 a power of 2?
If you're allowed to have real powers than 7 is a power of 2 and doesn't
satisfy your condition. Either way, you're either catching too many numbers
or not enough :).
Shorter: an even number is not necessarily a power of 2.
 
M

Mark McIntyre

Homework question...
int is_power_of_two(double x) {
return x > 0.0; /* one statement, no loops */
}

Cute. He probably meant an *integer* power? :)

Mark McIntyre
 
A

ashu

Jack said:
Hi:
There is question about C programming:
Use just one C statement to check whether a variable is the power of 2. No
loops allowed.
Waiting for your answer.I have not slept well a few days for this.
its very simple
see
any variable it is divided by 3,5,7 or 9 then it is not the power of 2
i.e,
main()
{
if (n%3 && n%5 && n%7 && n%9 != 0 )
printf("n is a power of 2");
}
 
R

Robert Gamble

ashu said:
its very simple
see
any variable it is divided by 3,5,7 or 9 then it is not the power of 2

By definition, a number that is a power of 2 has no prime factors other
than 2, your observation is an incomplete obfuscation of this fact.
Also, just so you know, any number evenly divisible by 9 is also evenly
divisible by 3 so the last part of your assertion is superfluous.
i.e,
main()
{
if (n%3 && n%5 && n%7 && n%9 != 0 )
printf("n is a power of 2");
}

Your example has numerious errors, both programatic and logical.
First, let's cover the programatic errors:
1. main returns an int so "int main (void)" would be a correct
declaration here.
2. You never define "n", let alone initialize it.
3. You need to #include <stdio.h> to use printf and you should have a
newline at the end of your string to be portable.
4. You should return an integer value from main.

It is true that any number evenly divisible by 3, 5, 7, or 9 is not a
power of 2, but to say that any number that does is not evenly
divisible by these numbers is a power of 2 is a non sequitur, by your
criteria 1, 11, 13, and would each be a power of 2.

Robert Gamble
 
R

Richard Heathfield

Robert Gamble said:
It is true that any number evenly divisible by 3, 5, 7, or 9 is not a
power of 2, but to say that any number that does is not evenly
divisible by these numbers is a power of 2 is a non sequitur,

No, it isn't. It's a false conclusion, not a non sequitur.
by your criteria 1, 11, 13, and would each be a power of 2.

1 *is* a power of 2! It is 2 to the power 0.
 
R

Robert Gamble

Richard said:
Robert Gamble said:


No, it isn't. It's a false conclusion, not a non sequitur.

It's a prime example of a non sequitur; Given: if A (number is
divisible by 3,5,7,9) then B (not a power of 2), then it does not
logically follow that NOT A (not divisible by 3,5,7,9) yields NOT B (is
a power of two). The type of this example is more specifically
referred to as Denying the Antecedent.
1 *is* a power of 2! It is 2 to the power 0.

Okay, I screwed that up. That should have read either "11, 13, and 17
would each" or "11 and 13 would both".

Robert Gamble
 

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
474,173
Messages
2,570,937
Members
47,481
Latest member
ElviraDoug

Latest Threads

Top