# Fast Exponention using Bit Manipulation

Given two integers A and N, the task is to calculate A raised to power N (i.e. AN).
Examples:

Input: A = 3, N = 5
Output: 243
Explanation:
3 raised to power 5 = (3*3*3*3*3) = 243

Input: A = 21, N = 4
Output: 194481
Explanation:
21 raised to power 4 = (21*21*21*21) = 194481

Naive Approach:
The simplest approach to solve this problem is to repetitively multiply A, N times and print the product.
Time Complexity: O(N)
Auxiliary Space: O(1)

Efficient Approach:
To optimize the above approach, the idea is to use Bit Manipulation. Convert the integer N to its binary form and follow the steps below:

• Initialize ans to store the final answer of AN.
• Traverse until N > 0 and in each iteration, perform Right Shift operation on it.
• Also, in each iteration, multiply A with itself and update it.
• If current LSB is set, then multiply current value of A to ans.
• Finally, after completing the above steps, print ans.

Below is the implementation of the above approach:

## C++

 `// C++ Program to implement ` `// the above appraoch ` `#include ` `using` `namespace` `std; ` ` `  `// Function to return a^n ` `int` `powerOptimised(``int` `a, ``int` `n) ` `{ ` ` `  `    ``// Stores final answer ` `    ``int` `ans = 1; ` ` `  `    ``while` `(n > 0) { ` ` `  `        ``int` `last_bit = (n & 1); ` ` `  `        ``// Check if current LSB ` `        ``// is set ` `        ``if` `(last_bit) { ` `            ``ans = ans * a; ` `        ``} ` ` `  `        ``a = a * a; ` ` `  `        ``// Right shift ` `        ``n = n >> 1; ` `    ``} ` ` `  `    ``return` `ans; ` `} ` ` `  `// Driver Code ` `int` `main() ` `{ ` `    ``int` `a = 3, n = 5; ` ` `  `    ``cout << powerOptimised(a, n); ` ` `  `    ``return` `0; ` `} `

## Java

 `// Java program to implement  ` `// the above appraoch  ` `class` `GFG{  ` ` `  `// Function to return a^n  ` `static` `int` `powerOptimised(``int` `a, ``int` `n)  ` `{  ` ` `  `    ``// Stores final answer  ` `    ``int` `ans = ``1``;  ` ` `  `    ``while` `(n > ``0``)  ` `    ``{  ` `        ``int` `last_bit = (n & ``1``);  ` ` `  `        ``// Check if current LSB  ` `        ``// is set  ` `        ``if` `(last_bit > ``0``) ` `        ``{  ` `            ``ans = ans * a;  ` `        ``}  ` `         `  `        ``a = a * a;  ` ` `  `        ``// Right shift  ` `        ``n = n >> ``1``;  ` `    ``}  ` `    ``return` `ans;  ` `}  ` ` `  `// Driver Code  ` `public` `static` `void` `main(String[] args)  ` `{  ` `    ``int` `a = ``3``, n = ``5``;  ` ` `  `    ``System.out.print(powerOptimised(a, n));  ` `} ` `} ` ` `  `// This code is contributed by PrinciRaj1992 `

## Python3

 `# Python3 program to implement ` `# the above approach ` ` `  `# Function to return a^n ` `def` `powerOptimised(a, n): ` `     `  `    ``# Stores final answer  ` `    ``ans ``=` `1` `     `  `    ``while` `(n > ``0``): ` `        ``last_bit ``=` `(n & ``1``) ` `         `  `        ``# Check if current LSB  ` `        ``# is set  ` `        ``if` `(last_bit): ` `            ``ans ``=` `ans ``*` `a ` `        ``a ``=` `a ``*` `a ` `         `  `        ``# Right shift  ` `        ``n ``=` `n >> ``1` `         `  `    ``return` `ans ` ` `  `# Driver code ` `if` `__name__ ``=``=` `'__main__'``: ` `     `  `    ``a ``=` `3` `    ``n ``=` `5` `     `  `    ``print``(powerOptimised(a,n)) ` ` `  `# This code is contributed by virusbuddah_ `

## C#

 `// C# program to implement  ` `// the above appraoch  ` `using` `System; ` ` `  `class` `GFG{  ` ` `  `// Function to return a^n  ` `static` `int` `powerOptimised(``int` `a, ``int` `n)  ` `{  ` `     `  `    ``// Stores readonly answer  ` `    ``int` `ans = 1;  ` ` `  `    ``while` `(n > 0)  ` `    ``{  ` `        ``int` `last_bit = (n & 1);  ` ` `  `        ``// Check if current LSB  ` `        ``// is set  ` `        ``if` `(last_bit > 0)  ` `        ``{  ` `            ``ans = ans * a;  ` `        ``}  ` `        ``a = a * a;  ` ` `  `        ``// Right shift  ` `        ``n = n >> 1;  ` `    ``}  ` `    ``return` `ans;  ` `}  ` ` `  `// Driver Code  ` `public` `static` `void` `Main(String[] args)  ` `{  ` `    ``int` `a = 3, n = 5;  ` ` `  `    ``Console.Write(powerOptimised(a, n));  ` `}  ` `}  ` ` `  `// This code is contributed by Princi Singh `

Output:

```243
```

Time Complexity: O(logN)
Auxiliary Space: O(1)

Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.

My Personal Notes arrow_drop_up Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.

Article Tags :
Practice Tags :

1

Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.