Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 11-01-2008, 09:42 PM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Jan 2008
Posts: 6
Default 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.
maitrasagnik is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 11-01-2008, 09:49 PM   #2 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default 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
QwertyManiac is offline  
Old 11-01-2008, 09:52 PM   #3 (permalink)
Right Off the Assembly Line
 
Join Date: Jan 2008
Posts: 6
Default Re: power function by bitwise operations

thnxxx qm.
maitrasagnik is offline  
Old 24-01-2008, 03:38 PM   #4 (permalink)
Right Off the Assembly Line
 
Join Date: Nov 2007
Posts: 16
Default Re: power function by bitwise operations

Can the power of any other number calculated through it.
manubatham20 is offline  
Old 24-01-2008, 05:01 PM   #5 (permalink)
Commander in Chief
 
QwertyManiac's Avatar
 
Join Date: Jul 2005
Posts: 6,658
Default 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
QwertyManiac is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

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


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
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:47 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2