Click here to Skip to main content
12,253,990 members (65,311 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as

Stats

8K views
68 downloads
6 bookmarked
Posted

Binary Exponentiation

, 6 Nov 2013 CPOL
Rate this:
Please Sign up or sign in to vote.
The idea with binary exponentiation is to use the binary representation of the exponent to reduce in log2 the number of iterations required to produce a result.

Introduction

Mathematical operations and functions can have a negative impact on the performance of an algorithm when the operands or arguments it includes possess a considerable time complexity. For instance, the multiplication of large numbers is a complex operation that is usually solved by algorithms that bypass the disadvantage of the traditional sum and provide a more effective and less time consuming method. The same happens with exponentiation when the exponent is a large number. The classical algorithm for exponentiation and the first that pops up into any programmer’s mind is pretty straightforward; it loops from 1 to exp (where exp is the exponent to raise the base) and multiplies the base by a variable result in each loop, similar to the following code.

The problem with the previous algorithm is that it has a high cost when the exp is a large number like 10000 or 100000. A more effective alternative could be achieved if the exponent is considered as a binary number and this approach is known as the binary exponentiation algorithm which runs in O(log(exp)) contrary to the classical version which runs in O(exp), obviously more expensive. The next code details the algorithm.

The idea with binary exponentiation is to use the binary representation of the exponent to reduce in log2 the number of iterations required to produce a result. A loop goes from left to right in the binary representation of the exponent and multiplies the value of a variable result by itself. This variable stores base^(2^b_m*b_k +, …, + 2^b_j*b_i = b’) (where b’ is the binary prefix discovered in the loop at a given moment) in order to produce the value (base^(2^b_m*b_k +, …, + 2^b_j*b_i = b’))^2 which equals base^(2^(b_m+1)*b_k +, …, + 2^(b_j+1)*b_i  + 2^(b_j)*b_i -1 = b’’). If a 1 digit is found then is also necessary to add base^1 to the result.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

arnaldo.skywalker
Software Developer
Cuba Cuba
I'm a programmer and mathematician, graduated of Computer Science. Lover of Jazz, music, cinema and art in general.

You may also be interested in...

Comments and Discussions

 
SuggestionString concatenation Pin
Richard Deeming12-Nov-13 8:17
professionalRichard Deeming12-Nov-13 8:17 
GeneralRe: String concatenation Pin
arnaldo.skywalker12-Nov-13 8:51
memberarnaldo.skywalker12-Nov-13 8:51 
GeneralRe: String concatenation Pin
Richard Deeming12-Nov-13 9:02
professionalRichard Deeming12-Nov-13 9:02 
GeneralRe: String concatenation Pin
arnaldo.skywalker12-Nov-13 9:10
memberarnaldo.skywalker12-Nov-13 9:10 
GeneralRe: String concatenation Pin
Richard Deeming12-Nov-13 9:26
professionalRichard Deeming12-Nov-13 9:26 
QuestionOptimality Pin
YvesDaoust11-Nov-13 2:33
memberYvesDaoust11-Nov-13 2:33 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.160426.1 | Last Updated 6 Nov 2013
Article Copyright 2013 by arnaldo.skywalker
Everything else Copyright © CodeProject, 1999-2016
Layout: fixed | fluid