Forum
Digit Technology Discussion Forum
>
Software
>
Programming
power function by bitwise operations
User Name
Remember Me?
Password
Register
FAQ
Calendar
Mark Forums Read
Programming
The destination for developers - C, C++, Java, Python and the lot
LinkBack
Thread Tools
Display Modes
11-01-2008, 09:42 PM
#
1
(
permalink
)
maitrasagnik
Right Off the Assembly Line
Join Date: Jan 2008
Posts: 6
power function by bitwise operations
can anyone tell me how to implement the function to calculate power of 2 with the help of bitwise operations.
Advertisements. Register and be a member of the community to get rid of them.
Advertisement
11-01-2008, 09:49 PM
#
2
(
permalink
)
QwertyManiac
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
Re: power function by bitwise operations
Left shift operator is all you need.
To calculate 2^n, do:
Code:
2<<(n-1)
n-1 cause 2<<0 is 2 itself.
Some examples:
Code:
(2 squared)
2<<1 4
(2 cubed)
2<<2 8
(2^4)
2<<3 16
(2^1)
2<<0 2
(2^11)
2<<10 2048
__________________
Harsh J
www.harshj.com
11-01-2008, 09:52 PM
#
3
(
permalink
)
maitrasagnik
Right Off the Assembly Line
Join Date: Jan 2008
Posts: 6
Re: power function by bitwise operations
thnxxx qm.
24-01-2008, 03:38 PM
#
4
(
permalink
)
manubatham20
Right Off the Assembly Line
Join Date: Nov 2007
Posts: 16
Re: power function by bitwise operations
Can the power of any other number calculated through it.
24-01-2008, 05:01 PM
#
5
(
permalink
)
QwertyManiac
Commander in Chief
Join Date: Jul 2005
Posts: 6,658
Re: power function by bitwise operations
No, it works out only for 2^n. For a^n there are other ways but none as efficient as this ..
__________________
Harsh J
www.harshj.com
Bookmarks
Digg
del.icio.us
StumbleUpon
Google
«
Previous Thread
|
Next Thread
»
Thread Tools
Show Printable Version
Email this Page
Display Modes
Linear Mode
Switch to Hybrid Mode
Switch to Threaded Mode
Posting Rules
You
may not
post new threads
You
may not
post replies
You
may not
post attachments
You
may not
edit your posts
BB code
is
On
Smilies
are
On
[IMG]
code is
On
HTML code is
Off
Trackbacks
are
On
Pingbacks
are
On
Refbacks
are
On
Forum Rules
Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
Arithmetic operations in flash
part_time_ch
Internet & WWW
1
20-09-2007
12:44 PM
Power of America & Power of India - Awesome [large pics]
s18000rpm
Chit-Chat
13
25-07-2007
04:49 PM
PC function
citizen
Software Q&A
5
09-12-2006
11:55 PM
Does the media have the right to perform sting operations against politicians??
freakanomics
Fight Club
11
31-08-2006
07:28 AM
Help with the IF Function in Excel
h1ghn3zz
Software Q&A
3
22-08-2006
09:36 AM
Latest Threads
Sniper Elite V2 discussion
- by
101gamzer
The LG Optimus One Thread
- by
noobdroid
Battlefield 3 Multiplayer Discussion
- by
Charan
Diablo III Discussion
- by
Gaurav Bhattacharjee
Must Watch Movies Recommended By Digitians
- by
~Phenom~
All Metalheads here
- by
MetalheadGautham
Official Android discussion thread
- by
amitabhishek
Dota 2
- by
Sarath
the FOOTBALL channel
- by
Arsenal_Gunners
Sony announces the water-proof Xperia go and Xperia acro S
- by
clmlbx
Advertisement
All times are GMT +5.5. The time now is
12:47 AM
.
Contact Us
-
Thinkdigit.com
-
Archive
-
Top
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.
LinkBack
LinkBack URL
About LinkBacks
Search Engine Optimization by
vBSEO
3.3.2