A C Question.

P

pete

Robert said:
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.


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.

And this expression
(n%3 && n%5 && n%7 && n%9 != 0 )
is written strangely.

Either
(n%3 && n%5 && n%7 && n%9)
or
(n%3 != 0 && n%5 != 0 && n%7 != 0 && n%9 != 0 )
would be better.
 
D

Dik T. Winter

> 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");
> }
>

Ah, yes, 316 is a power of 2.
 
J

Joe Wright

Robert said:
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.




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

Robert Gamble
Powers of 2

#include <stdio.h>

int main(void) {
int i, j, k = 0;
for (i = 1; i < 20; ++i) {
j = i & (i - 1);
if (j) printf("%2d\n", i);
else printf("%2d is 2 to the power %d\n", i, k++);
}
return 0;
}
 
K

Keith Thompson

pete said:
And this expression
(n%3 && n%5 && n%7 && n%9 != 0 )
is written strangely.

Either
(n%3 && n%5 && n%7 && n%9)
or
(n%3 != 0 && n%5 != 0 && n%7 != 0 && n%9 != 0 )
would be better.

In the context of doing someone's homework for him (which is how this
thread started), the original version is perfect.
 
C

CBFalconer

Joe said:
.... snip ...

#include <stdio.h>

int main(void) {
int i, j, k = 0;
for (i = 1; i < 20; ++i) {
j = i & (i - 1);
if (j) printf("%2d\n", i);
else printf("%2d is 2 to the power %d\n", i, k++);
}
return 0;
}

Funny thing. It seems that all values than 20 are powers of two on
my 1's complement machine. Where, oh where, did they come from?

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
P

Peter Nilsson

CBFalconer said:
Funny thing. It seems that all values than 20 are powers of two on
my 1's complement machine. Where, oh where, did they come from?

Your imagination?! ;-)
 
P

pete

Peter said:
Your imagination?! ;-)

"1's complement" as far as I know,
refers to the representation of negative integers,
so I don't see what CBFalconer is getting at.
 
C

CBFalconer

pete said:
"1's complement" as far as I know, refers to the representation of
negative integers, so I don't see what CBFalconer is getting at.

Peter Nilsson (and Joe) has it right. I went off less than
half-cocked. I was thinking of the effect of ((v) & (-v)).

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson
More details at: <http://cfaj.freeshell.org/google/>
 
M

Michael Wojcik

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.

Do you have a reference for this definition of non sequitur? I've
never seen it used to include errors of logic, and eg Lanham's _A
Handlist of Rhetorical Terms_ doesn't endorse that usage.

"non sequitur" of course literally means "it does not follow", so
it could be construed very broadly to include errors of logic (where
conclusions do not follow, in the logical sense, from propositions),
but I've only ever seen it applied to cases where a conclusion is
drawn from irrelevant arguments, and Lanham appears to agree.
 
R

Robert Gamble

Michael said:
Do you have a reference for this definition of non sequitur? I've
never seen it used to include errors of logic, and eg Lanham's _A
Handlist of Rhetorical Terms_ doesn't endorse that usage.

"non sequitur" of course literally means "it does not follow", so
it could be construed very broadly to include errors of logic (where
conclusions do not follow, in the logical sense, from propositions),
but I've only ever seen it applied to cases where a conclusion is
drawn from irrelevant arguments, and Lanham appears to agree.

Wikipedia has a description that is in tune with my understanding of
the term:
http://en.wikipedia.org/wiki/Non_sequitur_(logic)
There may well be a general usage of the word as well as the one
related to logical analysis, I am familiar only with the latter.

Robert Gamble
 
S

Shastri

Hi,
This can be one of the possible solutions

if(!(num & (num - 1)) && num)
{
// Power of 2!
}

Shastri
 

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