power of two...

J

Joachim Schmitz

Shraddha said:
How to find the power of two (2 rais to n) in a single step???
With the shift operator (for integer values) or with pow() (for double
values)

Bye, Jojo
 
J

Joachim Schmitz

Richard Heathfield said:
Shraddha said:


double single_step = pow(2, n);
Passing integer values to a function expecting double without a prototype in
sight?

Bye, Jojo
 
R

Richard Heathfield

Joachim Schmitz said:
Passing integer values to a function expecting double without a
prototype in sight?

The prototype is in <math.h>

Note also the absence of a function in which the code needs to be
enclosed.
 
B

Boudewijn Dijkstra

Op Mon, 11 Jun 2007 16:20:51 +0200 schreef Richard Heathfield
Joachim Schmitz said:

The prototype is in <math.h>

Note also the absence of a function in which the code needs to be
enclosed.

Note also the absence of a compilation unit (or file) in which the
function needs to be enclosed.
 
U

user923005

How to find the power of two (2 rais to n) in a single step???

I am guessing that what you are really asking is how to find out if an
integer number is an integral power of 2.
A simple way to do that is to do a bitcount and see if it is 1.

Maybe something like this:

int bitcount (unsigned long n)
{

n = (n & 0x55555555) + ((n >> 1) & 0x55555555);
n = (n & 0x33333333) + ((n >> 2) & 0x33333333);
n = (n + (n >> 4)) & 0x0f0f0f0f;
n += n >> 8;
n += n >> 16;
return (n & 0xff);
}

then...

if (bitcount(n) == 1) puts("It's a power of 2");
else puts("It is not a power of 2");
 

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,432
Messages
2,571,682
Members
48,796
Latest member
Greg L.

Latest Threads

Top