Another C Question

Pages: 1, 2
free web hosting

Read Latest Entries..: (Post #12) by yomi on Jan 26 2005, 04:35 AM. (Line Breaks Removed)
-x, pls note, 00000100 x11111100 -x-x is (~x)+1, they are equal.-x is not simply change the fist bit.Sure I am not guessing, I have point out I have given the CORRECT one.QUOTE(osknockout @ Jan 25 2005, 11:02 PM)Hmm....00000100 x10000100 -x      //Randall Hyde, art of assembly, signed numbers01111011 ~(-x)01111100 ~(-X)+101111100==00000100? Yomi, stop guessing I think that wor... read more.
Read the FIRST post of this Topic. - Express your Opinion! Contribute Knowledge :-).

Open Discussion > CONTRIBUTE > Computers > Programming Languages > C/C++ Programming

Another C Question

cse-icons
Hi Friends,

Here is another interesting C Problem.
The problem is to find whether an integer is a power of 2 or not.
This must be done in a single C statement.
I tried a little but have not figured it out as yet.


I have been thinking on the following lines:

I suppose it has something to do with bit manipulation coz.. all integers that are power of 2 will have only single 1 in binary form, we need to be able to count if it has only single one..

Is there ny way???

Thanx.

Reply

osknockout
So easy. tongue.gif
#include <cmath>
take a log of base 2 //don't remember the function, look it up on google
and take an integer test.

That should do it biggrin.gif

Reply

cse-icons
QUOTE(osknockout @ Jan 10 2005, 10:16 PM)
So easy. tongue.gif
#include <cmath>
take a log of base 2 //don't remember the function, look it up on google
and take an integer test.

That should do it biggrin.gif
*




Thanks. that is a good idea...
i'll check it out...
btw keep posted if u get an idea on how to do it without using built-in functions.

Reply

osknockout
that's not hard either biggrin.gif

Do a repeat x <= 1
{
number / 2;
x -= 1;
}

if x==1, then it's a power of two.

If you want optimization, well... specify.

Reply

cse-icons
hi osknockout,

That is a simple solution any one wud think of as first option...
but the condition in the question is not satisfied here. i.e., "it shoud be determined in a single C Statement"

so what i mean is some other solution in a single C Statement which does not use built in function...
the solution u have give has more than that....


Regards.

Reply

osknockout
Right, sorry, I'll post if I ever find out...

So the solution set is:
00000010
00000100
00001000
00010000
00100000
01000000
10000000
ad infinitum

Reply

yomi
I think this is more easy:

#define IS_2_POWER(X) (X==(~X)+1)

Reply

osknockout
QUOTE
#define IS_2_POWER(X) (X==(~X)+1)

Nice idea but...

00000010 = 2
11111101 = ~2
11111110 = ~2 + 1

x==(~X)+1?
don't think so.

Good attempt though smile.gif

I've GOT IT! (Thanks yomi)
CODE
#define IS_POWER_2(X) (!(2 mod x))
(1 >= 2 mod x + 4 mod x + 8 mod x + 16 mod x + 32 mod x + 64 mod x
+ 128 mod x) /*adjust number of 2^x digits as necessary*/


Ok, I'll work on a more optimized formula for right now...

Reply

yomi
oh, i made a mistake , think about this:
x==((~x)+1)|x

I am not very sure. But sure there is a simular way to achieve this operation with high performance.

QUOTE(osknockout @ Jan 22 2005, 12:45 PM)
Nice idea but...

00000010 = 2
11111101 = ~2
11111110 = ~2 + 1

x==(~X)+1?
don't think so.

Good attempt though smile.gif

I've GOT IT! (Thanks yomi)
CODE
#define IS_POWER_2(X) (!(2 mod x))
(1 >= 2 mod x + 4 mod x + 8 mod x + 16 mod x + 32 mod x + 64 mod x
+ 128 mod x) /*adjust number of 2^x digits as necessary*/


Ok, I'll work on a more optimized formula for right now...
*

 

 

 


Reply

osknockout
QUOTE
oh, i made a mistake , think about this:
x==((~x)+1)|x

Hmm...

00000010 x
11111101 ~x
11111110 (~x)+1
11111110 ((~x)+1)|x

00000010 == 11111110?

I'll think about that formula again...

Reply

Latest Entries

yomi
-x, pls note,
00000100 x
11111100 -x

-x is (~x)+1, they are equal.
-x is not simply change the fist bit.

Sure I am not guessing, I have point out I have given the CORRECT one.

QUOTE(osknockout @ Jan 25 2005, 11:02 PM)
Hmm....

00000100 x
10000100 -x      //Randall Hyde, art of assembly, signed numbers
01111011 ~(-x)
01111100 ~(-X)+1

01111100==00000100? Yomi, stop guessing laugh.gif
I think that works for 2 though.
*


Reply

osknockout
QUOTE
Now I can give the correct one:

X==~(-X)+1


Hmm....

00000100 x
10000100 -x //Randall Hyde, art of assembly, signed numbers
01111011 ~(-x)
01111100 ~(-X)+1

01111100==00000100? Yomi, stop guessing laugh.gif
I think that works for 2 though.

Reply



Got an Opinion! Express your Views! (no registration):-
Add your Reply/ Opinion/ Views/ Comments/ Suggestion/ Questions/ Queries etc.
Posts with decent grammar & English will be accepted and please refrain from profanities.
For asking a Question, We recommend you to sign-up (for free) so that you can track the topic easily.

Nature of your Post*: Opinion/ Reply/ Comments
Question/Query
Feedback to us.
       
Name   Email
Title/Question*

(Maximum characters: 10,000)
You have characters left.

Pages: 1, 2
Recent Queries:-
  1. is_power_2 - 338.95 hr back. (1)
  2. interesting c question - 378.78 hr back. (1)
Similar Topics
Looking for c, question

*RANDOM STUFF*





*SIMILAR VIDEOS*
Searching Video's for c, question

*MORE FROM TRAP17.COM*
advertisement



Another C Question



 

 

 

 

ADD REPLY / Got an Opinion! a humble request :-) RAPID SEARCH! Free Hosting [X]
Express your Opinions, Thoughts or Contribute your information that might help someone here.
Ask your Doubts & Queries to get answers.. "Together, We enlight each other!"
Register FREE for AD-FREE forum, Create your own topics, Ask Questions, track topics, setup subscriptions & notifications and Get a Free Website w/ Email and FTP.
500MB Space *No Ads*, CPanel, FTP, PHP, MySQL, EMails - 100% FREE