cse-icons 0 Report post Posted January 10, 2005 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. Share this post Link to post Share on other sites
osknockout 0 Report post Posted January 10, 2005 So easy. #include <cmath>take a log of base 2 //don't remember the function, look it up on googleand take an integer test.That should do it Share this post Link to post Share on other sites
cse-icons 0 Report post Posted January 11, 2005 So easy. #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 <{POST_SNAPBACK}> 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. Share this post Link to post Share on other sites
osknockout 0 Report post Posted January 11, 2005 that's not hard either :rolleyes: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. Share this post Link to post Share on other sites
cse-icons 0 Report post Posted January 13, 2005 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. Share this post Link to post Share on other sites
osknockout 0 Report post Posted January 14, 2005 Right, sorry, I'll post if I ever find out...So the solution set is:00000010000001000000100000010000001000000100000010000000ad infinitum Share this post Link to post Share on other sites
yomi 0 Report post Posted January 22, 2005 I think this is more easy:#define IS_2_POWER(X) (X==(~X)+1) Share this post Link to post Share on other sites
osknockout 0 Report post Posted January 22, 2005 #define IS_2_POWER(X) (X==(~X)+1)Nice idea but...00000010 = 211111101 = ~211111110 = ~2 + 1x==(~X)+1?don't think so.Good attempt though I've GOT IT! (Thanks yomi)#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... Share this post Link to post Share on other sites
yomi 0 Report post Posted January 23, 2005 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. Nice idea but... 00000010 = 2 11111101 = ~2 11111110 = ~2 + 1 x==(~X)+1? don't think so. Good attempt though I've GOT IT! (Thanks yomi) #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... 42415[/snapback] Share this post Link to post Share on other sites
osknockout 0 Report post Posted January 23, 2005 oh, i made a mistake , think about this:x==((~x)+1)|xHmm...00000010 x11111101 ~x11111110 (~x)+111111110 ((~x)+1)|x00000010 == 11111110?I'll think about that formula again... Share this post Link to post Share on other sites
yomi 0 Report post Posted January 24, 2005 Now I can give the correct one: X==~(-X)+1 Hmm... 00000010 x 11111101 ~x 11111110 (~x)+1 11111110 ((~x)+1)|x 00000010 == 11111110? I'll think about that formula again... 42694[/snapback] Share this post Link to post Share on other sites
osknockout 0 Report post Posted January 25, 2005 Now I can give the correct one:X==~(-X)+1 Hmm....00000100 x10000100 -x //Randall Hyde, art of assembly, signed numbers01111011 ~(-x)01111100 ~(-X)+101111100==00000100? Yomi, stop guessing I think that works for 2 though. Share this post Link to post Share on other sites
yomi 0 Report post Posted January 26, 2005 -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. Hmm.... 00000100 x 10000100 -x //Randall Hyde, art of assembly, signed numbers 01111011 ~(-x) 01111100 ~(-X)+1 01111100==00000100? Yomi, stop guessing I think that works for 2 though. 43345[/snapback] Share this post Link to post Share on other sites