top button
Flag Notify
    Connect to us
      Site Registration

Site Registration

How to implement mathematical power function in C ?

+2 votes
121 views
How to implement mathematical power function in C ?
posted Feb 17, 2015 by Vikram Singh

Share this question
Facebook Share Button Twitter Share Button LinkedIn Share Button

1 Answer

0 votes

This is one way Exponentiation by squaring.

int ipow(int base, int exp)
{
    int result = 1;
    while (exp)
    {
        if (exp & 1)
            result *= base;
        exp >>= 1;
        base *= base;
    }

    return result;
}

This is the standard method for doing modular exponentiation for huge numbers in asymmetric cryptography.

answer Feb 18, 2015 by Manikandan J
Similar Questions
0 votes

Implement power function using C/C++? The function should take two numbers as input (e.g. 2,3) and return 8 as output which is 2^3?

0 votes

How can I write an ASSEMBLER in C programming language for calculating mathematical operations?

+6 votes

For example draw a binary Tree for the expression:

A * B - (C + D) * (P / Q)
...