Last active
January 17, 2017 02:52
-
-
Save JeonghunLee/3d50f03706e29a71ef961cea2097118a to your computer and use it in GitHub Desktop.
This file contains 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
#include <stdio.h> | |
//http://stackoverflow.com/questions/101439/the-most-efficient-way-to-implement-an-integer-based-power-function-powint-int | |
/* | |
all exponential values (e.g.5) was chaged to 2 system | |
* check even or odd | |
<-----------------------> | |
2^3 2^2 2^1 2^0 | |
0b 1 0 1 0 : 10 | |
0b 0 1 0 1 : 05 | |
<-----------------------> | |
base *= base : 2 ^ x | |
*/ | |
int ipow(int base, int exp) | |
{ | |
int result = 1; | |
while (exp) | |
{ | |
if (exp & 1) // exp is even | |
result *= base; | |
exp >>= 1; | |
base *= base; // 2 ^ x | |
} | |
return result; | |
} | |
int main(void) { | |
int ret; | |
ret = ipow(5,5); | |
printf("5 5 %d \n",ret); | |
ret = ipow(5,10); | |
printf("5 10 %d \n",ret); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment