Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Semrush - keyword research tool

Turn ChatGPT, Claude, Gemini, And CoPilot Into Your Personal Assistant, Business Coach, Content Creator, And More

AFFILIATE MARKETING Your all-in-one performance engine Manage affiliates, creators, and customer referrals in one unified platform—turning every partnership into measurable growth
Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

42,709 questions

55,473 answers

573 users

How to implement an integer-based power function pow(int, int) in C++

2 Answers

0 votes
#include <iostream>

// Function to calculate integer power
int intpow(int base, int exp) {
    int power = 1;

    while (true) {
        if (exp & 1)
            power *= base;
        exp >>= 1;
        if (!exp)
            break;
        base *= base;
    }

    return power;
}

int main() {
    std::cout << intpow(2, 3) << std::endl;  // 8
    std::cout << intpow(3, 3) << std::endl;  // 27
    std::cout << intpow(3, 2) << std::endl;  // 9
    std::cout << intpow(2, 2) << std::endl;  // 4
    std::cout << intpow(5, 2) << std::endl;  // 25
    std::cout << intpow(-2, 4) << std::endl; // 16
}



/*
run:

8
27
9
4
25
16

*/

 



answered Jun 10, 2025 by avibootz
0 votes
#include <iostream>
#include <stdexcept>

/*
    Integer power function: intpow(base, exponent)

    - Computes base^exponent for integer inputs.
    - Uses exponentiation by squaring:
        • This reduces the number of multiplications dramatically.
        • Runs in O(log exponent) time.
    - Handles negative bases naturally.
    - Rejects negative exponents because the result would be fractional.
      (You can extend this if you want integer reciprocals or fixed‑point.)
*/

int intpow(int base, int exp) {
    // Guard against negative exponents: not representable as int
    if (exp < 0) {
        throw std::invalid_argument("intpow: negative exponent not supported");
    }

    // Fast path: anything to the power of 0 is 1
    if (exp == 0) {
        return 1;
    }

    // Use exponentiation by squaring
    long long result = 1;     // use wider type internally to reduce overflow risk
    long long current = base; // current multiplier

    while (exp > 0) {
        // If the current exponent bit is set, multiply result by current
        if (exp & 1) {
            result *= current;
        }

        // Square the current multiplier for the next bit
        current *= current;

        // Shift exponent right by one bit
        exp >>= 1;
    }

    // Cast back to int; caller is responsible for avoiding overflow
    return static_cast<int>(result);
}

/*
    Test main
*/
int main() {
    std::cout << intpow(2, 3) << std::endl;  // 8
    std::cout << intpow(3, 3) << std::endl;  // 27
    std::cout << intpow(3, 2) << std::endl;  // 9
    std::cout << intpow(2, 2) << std::endl;  // 4
    std::cout << intpow(5, 2) << std::endl;  // 25
    std::cout << intpow(-2, 4) << std::endl; // 16
}



/*
run:

8
27
9
4
25
16

*/

 



answered 8 hours ago by avibootz
edited 8 hours ago by avibootz
...