Click here to Skip to main content
15,897,371 members
Articles / Programming Languages / C#
Tip/Trick

Binary Exponentiation

Rate me:
Please Sign up or sign in to vote.
5.00/5 (3 votes)
6 Nov 2013CPOL2 min read 21.6K   105   7   6
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.

Image 1

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.

Image 2

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)


Written By
Software Developer
Serbia Serbia
Computer Scientist and book author living in Belgrade and working for a German IT company. Author of Practical Artificial Intelligence: Machine Learning, Bots, and Agent Solutions Using C# (Apress, 2018) and PrestaShop Recipes (Apress, 2017). Lover of Jazz and cinema Smile | :)

Comments and Discussions

 
SuggestionString concatenation Pin
Richard Deeming12-Nov-13 8:17
mveRichard Deeming12-Nov-13 8:17 
GeneralRe: String concatenation Pin
Arnaldo P. Castaño12-Nov-13 8:51
professionalArnaldo P. Castaño12-Nov-13 8:51 
GeneralRe: String concatenation Pin
Richard Deeming12-Nov-13 9:02
mveRichard Deeming12-Nov-13 9:02 
Strings in .NET are immutable. When you concatenate two strings, the system has to allocate a new string which is large enough for the result, copy the characters from the first string to the new string, copy the characters from the second string to the new string, and then return the new string. Doing this in a loop is notoriously inefficient.

Your implementation of ToBinary allocates 64 string variables, and copies up to 1056 characters, to build a final string of up to 32 characters.

Using a loop, you avoid the string concatenation, so you don't allocate any strings or copy any characters.



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


GeneralRe: String concatenation Pin
Arnaldo P. Castaño12-Nov-13 9:10
professionalArnaldo P. Castaño12-Nov-13 9:10 
GeneralRe: String concatenation Pin
Richard Deeming12-Nov-13 9:26
mveRichard Deeming12-Nov-13 9:26 
QuestionOptimality Pin
YvesDaoust11-Nov-13 2:33
YvesDaoust11-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.