|
|
|
|
![]() ![]() |
Jan 10 2005, 12:01 PM
Post
#1
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 351 Joined: 19-October 04 From: India Member No.: 1,824 |
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. |
|
|
|
Jan 10 2005, 10:16 PM
Post
#2
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 399 Joined: 14-November 04 From: Elysium Member No.: 2,280 |
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 |
|
|
|
Jan 11 2005, 08:56 AM
Post
#3
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 351 Joined: 19-October 04 From: India Member No.: 1,824 |
QUOTE(osknockout @ Jan 10 2005, 10:16 PM) 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 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. |
|
|
|
Jan 11 2005, 10:24 PM
Post
#4
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 399 Joined: 14-November 04 From: Elysium Member No.: 2,280 |
that's not hard either
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. |
|
|
|
Jan 13 2005, 06:10 AM
Post
#5
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 351 Joined: 19-October 04 From: India Member No.: 1,824 |
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. |
|
|
|
Jan 14 2005, 10:35 PM
Post
#6
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 399 Joined: 14-November 04 From: Elysium Member No.: 2,280 |
Right, sorry, I'll post if I ever find out...
So the solution set is: 00000010 00000100 00001000 00010000 00100000 01000000 10000000 ad infinitum |
|
|
|
Jan 22 2005, 05:31 AM
Post
#7
|
|
|
Engineer ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 78 Joined: 25-July 04 From: China Member No.: 204 |
I think this is more easy:
#define IS_2_POWER(X) (X==(~X)+1) |
|
|
|
Jan 22 2005, 12:45 PM
Post
#8
|
|
|
Super Member ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 399 Joined: 14-November 04 From: Elysium Member No.: 2,280 |
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 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... |
|
|
|
Jan 23 2005, 03:41 PM
Post
#9
|
|
|
Engineer ![]() ![]() ![]() ![]() ![]() Group: Members Posts: 78 Joined: 25-July 04 From: China Member No.: 204 |
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 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... |