Binary Exponentiation
is a technique of computing a number raised to some quantity, which can be as small as
Scope of article
- This article explains what is
binary exponetiation
, where it can be used and it’s implementation. - This article shows the implementation of
binary exponentiation
in C++ only.
Takeaways
- Complexity of
Binary exponentiation
Time complexity
–
Introduction of Binary Exponentiation
Exponentiation means raising a number to a power. If we raise

If we look carefully at the definition, it means that to find a raised to the power of b or
There can be two ways to compute exponentiation : iterative method by using a for loop or by using recursion. Let us see both of these methods in detail.
Iterative Way
In this method, we use a for loop to multiply a with itself b times. As we use a for loop to iterate b times
, it is called the iterative method
.
#include <iostream>
using namespace std;
int main() {
// we need to compute a^b
int a = 4, b = 8;
// this will contain the final answer
int answer = 1;
// the for loop iterates b times
for(int i=1; i<=b; i++){
// we multiply answer with a for each iteration
answer = answer*a;
}
// answer contains required a^b
cout<<"4 raised to the power 8 is : "<<answer<<endl;
return 0;
}
Output
4 raised to the power 8 is : 65536
As we can see in the above code, the for loop runs a total of
The for loop runs
Recursive Way
In this method, we use recursion to compute
#include <iostream>
using namespace std;
int exponent(int a, int b){
// base case, when b = 0
if(b==0){
// any number raised to 0 is always 1
return 1;
}
//else we recurse for b-1 after multiplying the answer with a and return the answer
// now answer stores a^b
int answer = a * exponent(a,b-1);
return answer;
}
int main() {
// we need to compute a^b
int a = 4, b = 8;
// call the recursive function to compute a^b
cout<<"4 raised to the power 8 is : "<<exponent(a,b)<<endl;
return 0;
}
Output
4 raised to the power 8 is : 65536
In this case, we recurse until 0
, that means we call the recursive function a total of
So, this makes the time complexity of exponentiation as
Now, if
So, when dealing with large values of
Why Binary Exponentiation?
If we can somehow find a way to make our exponentiatio
If we can somehow find a way to make our exponentiation faster, we would be able to compute Binary Exponentiation
does.
By using Binary Exponentiation
, we can compute
Let us explore the intuition behind Binary Exponentiation and also its algorithm in the next section.
Algorithm
How does Binary Exponentiation
achieve logarithmic time complexity?
Let us explore the intuition behind the algorithm before we define it.
Intuition
Binary Exponeniation
uses the property of exponentiation and the fact that any number can be represented as sum of powers of two depending on its binary notation, to compute the final answer.
According to the property of exponentiation,
This means, that if we want to compute
Now if we look at b
in its binary representation
, we will be able to write b as a sum of powers of two.
As an example let us take a = 5
and b = 12
.12
in binary is represented as :
In powers of two, we can re-write it as

In other words 12
can be represented as a sum of
So, if we want to compute a = 5
and b = 12
, we can rewrite it as :
In other words, the problem boils down to representing
Like, here as 12
can be expressed as a sum of
But here again, computing 4
operations, and computing (4+8+1) = 13
operations, which is not an improvement over the previous method.
The thing to notice here is that now we have to only compute x
is a power of two, to compute
This follows directly from one other property of exponents, which is :
So, if we have b
is a power of two, then to get the value 2
, we just need to compute
Consider the previous example of 5
raised to powers of 2
starting from 0
. So, we compute
Hence, we can compute the values of 4
operations and only one more operation (multiplying
Time Complexity
So, we get to the final answer in just 5
operations, as opposed to 12 operations if we did the exponentiation the brute force way.
The final complexity of this algorithm turns out to be 2
, and multiplying them together will take more
Hence, the overall complexity is b
is the exponent.
Actual Algorithm
So, we start by computing
To know if the nth bit in 1 bit
shifted by n
, i.e, 2
during iterations and just find out its remainder every time to check if the bit is set or not. This is equivalent to converting
Note in the algorithm, that we only multiply
The algorithm to compute
POWER(a,b) :
if b==0 :{
return 1
}
else if b is odd :{
//this bit is set in binary representation of b as remainder will be 1
return a*POWER(a,(b-1)/2)*POWER(a,(b-1)/2)
}
else :{
//this bit is not set in the binary representation of b as remainder is 0
return POWER(a,b/2)*POWER(a,b/2)
}
This algorithm can be implemented by recursion as well as in the iterative method. Both of these have been described in detail in the next section.
Implementation of Binary Exponentiation
There are two approaches to the above defined algorithm, both of which we will discuss in detail in this section.
Recursive Approach
A recursive approach
is one in which the recursive function calls itself with slightly smaller parameter, until the base case is reached.
For binary exponentiation, our base case will be when our exponent, i.e b
in 0
. In such a case, we simply return 1
irrespective of a
.
For all other cases, we follow the algorithm defined above. If the current bit in b
is set, that is if b leaves a remainder of one when divided by two, then we recurse for (b-1)/2
and multiply it twice with a and return it. Else, b
gives a remainder zero when divided by 2
and hence the current bit is unset, so we just recurse for b/2
and multiply it twice and return it.
“`cpp
include
using namespace std;
//recursive function to calculate a^b
int power (int a, int b){
//base case
if(b==0){
return 1;
}
//if b is odd
else if(b%2==1){
return apower(a,(b-1)/2)power(a,(b-1)/2);
}//if b is even
else{
return power(a,b/2)*power(a,b/2);
}
}
int main() {
//the exponent to be calculated
int a = 5 , b = 12;
//output the answer
cout<<"5 raised to the power 12 is : "<<power(a,b)<<endl;
return 0;
}
### Iterative Approach
In the `iterative approach` we follow the same logic, but without recursion. Instead, we use a while loop to perform the same operations, until `b` is greater than `0`.
cpp
include
using namespace std;
int main() {
// the exponent we want to calculate
int a = 5 , b = 12;
//we will store the answer in this variable
int answer = 1;
//iterate till b>0
while(b>0){
//if b is odd, then this bit is set
if(b%2==1){
//in this case, multiply the current power of a to the answer
answer = answer*a;
//compute the sqaure of current a
a = a*a;
}
//b is even, so the current bit is not set
else{
//in this case, we dont need to multiply anything to the answer
//we just compute the sqaure of a
a = a*a;
}
//the current bit has been analysed, we move to the next bit
//so we divide b by 2
b = b/2;
}
//output the answer
cout<<"5 raised to the power 12 is : "<<answer<<endl;
return 0;
}
“`
The time complexity
of both these solutions is the same and equal to
Applications
- In
cryptography
, large exponents with modulo of a number are widely used. To compute large exponents, binary exponentiation is a fast method which finds an application in this field. - We can efficiently compute large exponents modulo some large number by this technique. This particular application is useful in solving many mathematical problems.
- This can be used to compute
modular multiplicative inverse
of a number. - Basically, this method is very useful for problems where we need to compute exponentiation in a speedy fashion.
Conclusion
Binary Exponentiation
is a technique of computing a number raised to some quantity in a fast and efficient manner.- It uses properties of exponentiation and binary numbers for fast computation.
- It has time complexity of
O(log(b))
whereb
is the power any number is being raised to. - It finds its applications in cryptography and in other mathematical problems where computation of exponents is required.
Ignite Data Structures Brilliance! Enroll in Scaler Academy’s Online DSA Course to Hone Your Coding Skills.