By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,330 Members | 1,572 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,330 IT Pros & Developers. It's quick & easy.

power of two...

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

Jun 11 '07 #1
Share this Question
Share on Google+
12 Replies


P: n/a
Shraddha wrote:
How to find the power of two (2 rais to n) in a single step???
Start with Google, it will help you with spelling.
Jun 11 '07 #2

P: n/a
"Shraddha" <sh*************@gmail.comschrieb im Newsbeitrag
news:11*********************@i38g2000prf.googlegro ups.com...
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
Jun 11 '07 #3

P: n/a
Shraddha said:
How to find the power of two (2 rais to n) in a single step???
double single_step = pow(2, n);

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 11 '07 #4

P: n/a
"Richard Heathfield" <rj*@see.sig.invalidschrieb im Newsbeitrag
news:9P*********************@bt.com...
Shraddha said:
>How to find the power of two (2 rais to n) in a single step???

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

Bye, Jojo
Jun 11 '07 #5

P: n/a
On Jun 11, 6:29 pm, Shraddha <shraddhajosh...@gmail.comwrote:
How to find the power of two (2 rais to n) in a single step???
I think you mean to find whether the given number is a power of two in
a single step,
which is a frequently asked question (especially in tech interviews of
Indian companies).
Read this: http://prokutfaq.byethost15.com/PowerOfTwo

Jun 11 '07 #6

P: n/a
Shraddha <sh*************@gmail.comwrites:
How to find the power of two (2 rais to n) in a single step???
As an unsigned long int: 1UL << n.
As a double: pow(2.0, n).
--
"If I've told you once, I've told you LLONG_MAX times not to
exaggerate."
--Jack Klein
Jun 11 '07 #7

P: n/a
Joachim Schmitz said:
"Richard Heathfield" <rj*@see.sig.invalidschrieb im Newsbeitrag
news:9P*********************@bt.com...
>Shraddha said:
>>How to find the power of two (2 rais to n) in a single step???

double single_step = pow(2, n);
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.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: rjh at the above domain, - www.
Jun 11 '07 #8

P: n/a
Op Mon, 11 Jun 2007 16:20:51 +0200 schreef Richard Heathfield
<rj*@see.sig.invalid>:
Joachim Schmitz said:
>"Richard Heathfield" <rj*@see.sig.invalidschrieb im Newsbeitrag
news:9P*********************@bt.com...
>>Shraddha said:

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

double single_step = pow(2, n);
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.
Note also the absence of a compilation unit (or file) in which the
function needs to be enclosed.

--
Gemaakt met Opera's revolutionaire e-mailprogramma:
http://www.opera.com/mail/
Jun 11 '07 #9

P: n/a

"Shraddha" <sh*************@gmail.comwrote in message
news:11*********************@i38g2000prf.googlegro ups.com...
How to find the power of two (2 rais to n) in a single step???
Look at the << operator
--
Fred L. Kleinschmidt
Jun 11 '07 #10

P: n/a
On Jun 11, 6:29 am, Shraddha <shraddhajosh...@gmail.comwrote:
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");

Jun 11 '07 #11

P: n/a
user923005 <dc*****@connx.comwrites:
On Jun 11, 6:29 am, Shraddha <shraddhajosh...@gmail.comwrote:
>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.
x != 0 && !(x & (x - 1))
--
"Your correction is 100% correct and 0% helpful. Well done!"
--Richard Heathfield
Jun 11 '07 #12

P: n/a
Shraddha wrote:
>
How to find the power of two (2 rais to n) in a single step???
Examine the value of n. The result is pre-known. You may find an
array of values useful.

value = poweroftwo[n];

--
<http://www.cs.auckland.ac.nz/~pgut001/pubs/vista_cost.txt>
<http://www.securityfocus.com/columnists/423>
<http://www.aaxnet.com/editor/edit043.html>
<http://kadaitcha.cx/vista/dogsbreakfast/index.html>
cbfalconer at maineline dot net

--
Posted via a free Usenet account from http://www.teranews.com

Jun 11 '07 #13

This discussion thread is closed

Replies have been disabled for this discussion.