Go Back   Velocity Reviews > Newsgroups > C Programming
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

Reply

C Programming - test whether a number is a power of 2

 
Thread Tools Search this Thread
Old 09-28-2003, 07:21 PM   #1
Default test whether a number is a power of 2


Give a one-line C expression to test whether a number is a power of 2.
No loops allowed.


Matt
  Reply With Quote
Old 09-28-2003, 07:37 PM   #2
Joona I Palaste
 
Posts: n/a
Default Re: test whether a number is a power of 2
Matt <> scribbled the following:
> Give a one-line C expression to test whether a number is a power of 2.
> No loops allowed.


((((x&&!(x&(x-!!x)))+x)-x)^x)^x

What do I win?

--
/-- Joona Palaste () ---------------------------\
| Kingpriest of "The Flying Lemon Tree" G++ FR FW+ M- #108 D+ ADA N+++|
| http://www.helsinki.fi/~palaste W++ B OP+ |
\----------------------------------------- Finland rules! ------------/
"Remember: There are only three kinds of people - those who can count and those
who can't."
- Vampyra


Joona I Palaste
  Reply With Quote
Old 09-28-2003, 07:55 PM   #3
Ivan Vecerina
 
Posts: n/a
Default Re: test whether a number is a power of 2
"Matt" <> wrote in message
news: om...
> Give a one-line C expression to test whether a number is a power of 2.
> No loops allowed.


isPow2 = x && !( (x-1) & x );

See http://www.caam.rice.edu/~dougm/twiddle/
for some related info.

hth
--
http://ivan.vecerina.com
http://www.brainbench.com <> Brainbench MVP for C++






Ivan Vecerina
  Reply With Quote
Old 09-28-2003, 10:03 PM   #4
Keith Thompson
 
Posts: n/a
Default Re: test whether a number is a power of 2
(Matt) writes:
> Give a one-line C expression to test whether a number is a power of 2.
> No loops allowed.


We're not here to do your homework for you.

--
Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <*> <http://www.sdsc.edu/~kst>
Schroedinger does Shakespeare: "To be *and* not to be"


Keith Thompson
  Reply With Quote
Old 09-28-2003, 10:40 PM   #5
jacob navia
 
Posts: n/a
Default Re: test whether a number is a power of 2

> isPow2 = x && !( (x-1) & x );


Algorithm:
If x is a power of two, it doesn't have any bits in common with x-1, since it
consists of a single bit on. Any positive power of two is a single bit, using
binary integer representation.

This means that we test if x-1 and x doesn't share bits with the and operator.

Of course, if x is zero (not a power of two) this doesn't hold, so we add an
explicit test for zero with xx && expression.

Negative powers of two (0.5, 0.25, 0.125, etc) could share this same property
in a suitable fraction representation. 0.5 would be 0.1, 0.250 would be 0.01,
0.125 would be 0.001 etc.

jacob




jacob navia
  Reply With Quote
Old 09-29-2003, 01:58 PM   #6
Aishwarya
 
Posts: n/a
Default Re: test whether a number is a power of 2
Joona I Palaste <> wrote in message news:<bl79og$2tv$>...
> Matt <> scribbled the following:
> > Give a one-line C expression to test whether a number is a power of 2.
> > No loops allowed.

>
> ((((x&&!(x&(x-!!x)))+x)-x)^x)^x
>
> What do I win?



hi Joona,

i know this works because i just tested it .. but help me understand
the algoritm please ..

Thanks and Regards
A.


Aishwarya
  Reply With Quote
Old 09-29-2003, 03:49 PM   #7
Eric Sosman
 
Posts: n/a
Default Re: test whether a number is a power of 2
Matt wrote:
>
> Give a one-line C expression to test whether a number is a power of 2.
> No loops allowed.


true_or_false = a_number > 0;

(For example, 3.14159 ~= 2 ** 1.65149.)

--



Eric Sosman
  Reply With Quote
Old 09-29-2003, 03:57 PM   #8
Dan Pop
 
Posts: n/a
Default Re: test whether a number is a power of 2
In <> Keith Thompson <> writes:

> (Matt) writes:
>> Give a one-line C expression to test whether a number is a power of 2.
>> No loops allowed.

>
>We're not here to do your homework for you.


OTOH, the solution is far too tricky for a homework assignment. It
requires a bit of knowledge that has precious little to do with C
programming.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email:


Dan Pop
  Reply With Quote
Old 09-29-2003, 04:08 PM   #9
Dan Pop
 
Posts: n/a
Default Re: test whether a number is a power of 2
In <> (Aishwarya) writes:

>Joona I Palaste <> wrote in message news:<bl79og$2tv$>...
>> Matt <> scribbled the following:
>> > Give a one-line C expression to test whether a number is a power of 2.
>> > No loops allowed.

>>
>> ((((x&&!(x&(x-!!x)))+x)-x)^x)^x

>
>i know this works because i just tested it .. but help me understand
>the algoritm please ..


Had you engaged your brain, you'd have easily noticed that one ^x cancels
the other and that -x cancels the +x, so you're left with

x&&!(x&(x-!!x))

Now, !!x is an obfuscated way of writing 1, when you know that x cannot
be zero (it was already tested), so, we're left with:

x&&!(x&(x-1))

The tricky bit is x&(x-1) which actually clears the least significant
bit that happens to be set. If the original number was a power of two,
it had only one bit set, so the result of x&(x-1) must be zero in such
cases.

The rest should be obvious.

Dan
--
Dan Pop
DESY Zeuthen, RZ group
Email:


Dan Pop
  Reply With Quote
Old 09-29-2003, 04:17 PM   #10
Jirka Klaue
 
Posts: n/a
Default Re: test whether a number is a power of 2
Aishwarya wrote:
> Joona I Palaste <> wrote in message news:<bl79og$2tv$>...

....
>>((((x&&!(x&(x-!!x)))+x)-x)^x)^x

>
> i know this works because i just tested it .. but help me understand
> the algoritm please ..


+x -x does nothing, ^x ^x does nothing.

remains:
x && !(x & (x - !!x))

!!x ist alway 1, because x can't be 0 at this point.

remains:
x && !(x & (x - 1))

Jirka



Jirka Klaue
  Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
On Your Way to Success: the Test 70-640 jiajiainlove@gmail.com MCTS 1 03-16-2009 11:47 AM
On Your Way to Success: the Test 70-640 jiajiainlove@gmail.com MCITP 0 03-16-2009 01:51 AM
reporting power with dc_shell perseo Hardware 0 10-11-2007 09:01 AM
Judge: File-swapping tools are legal Citizen Bob DVD Video 140 11-08-2006 06:42 PM
Power Supply Requirements Joe A+ Certification 1 12-20-2003 06:13 PM




SEO by vBSEO 3.3.2 ©2009, Crawlability, Inc.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46