Created
July 3, 2013 03:09
-
-
Save riyadparvez/5915147 to your computer and use it in GitHub Desktop.
Implementation of exponentiation by squaring in C#. See http://en.wikipedia.org/wiki/Exponentiation_by_squaring
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public static int PowerBySquaring(int baseNumber, int exponent) | |
| { | |
| int result = 1; | |
| while (exponent != 0) | |
| { | |
| if ((exponent & 1)==1) | |
| { | |
| result *= baseNumber; | |
| } | |
| exponent >>= 1; | |
| baseNumber *= baseNumber; | |
| } | |
| return result; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.