473,396 Members | 1,966 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

power of two...

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

Jun 11 '07 #1
12 11161
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
"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
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
"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
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
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
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
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

"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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Stephane Belzile | last post by:
Is there a way I can detect in vb.Net the power has switched to a UPS unit in case of power failure? Thanks
12
by: Roman Töngi | last post by:
I just want to raise a number to a power. e.g.: 16^2 The pow()-function of cmath seems to be not enough. I've read about valarray. Is it really so difficult? Can you give me an example. ...
0
by: Thiva Charanasri | last post by:
http://www.poweroflanguage.org Track: Computer Language 1st World Congress on the Power of Language: Theory, Practice and Performance Date: March 6 - 10, 2006 Bangkok, Thailand On this...
0
by: Thiva Charanasri | last post by:
http://www.poweroflanguage.org Track: Computer Language 1st World Congress on the Power of Language: Theory, Practice and Performance Date: March 6 - 10, 2006 Bangkok, Thailand On this...
1
by: Richard Fennell | last post by:
Am trying to do ASP.NET development on my XP Prof. box logged in as a power user (not the administrator I used to be). As an admin user all is OK. But as a power user, I have made sure my...
5
by: mathon | last post by:
hi, i develeoped a recursive function for the power and it works fine double power(double x, int n) { if (n < 0) return 1/power(x, -n); else if (n == 0) return 1.0;
9
by: mathon | last post by:
hi, i already posted an entry because of this problem, unfortunately i havent solved it so far..:( I have created a recursion for the calculation of the power like this: double...
6
by: maxthebaz | last post by:
Our machines have this requirement: if power failure occurs, many important variables are to be resumed from where they were interrupted after the machine is restarted (power on in this case). In...
3
by: greek | last post by:
the question is to calculate x^n(x power n) (whr n can be +ve or -ve) by using recursion. the algorithm is x= 1, n=0 1/x^n, n<0 x*x^(n-1), n>0 ...
8
by: skanemupp | last post by:
how do i solve power(5,1.3)? def power(nbr, po): if po==0: return 1 if po>0: return nbr*power(nbr, po-1) if po<0: return 1/power(nbr, -1*po)
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.