bcpow

bcpow(string $num, string $exponent, ?int $scale = null): string

An arbitrary-precision number raised to an integer power: results like 2^64, far beyond the int range, come back exactly as strings.

C C23

C23
#include <stdio.h>

/* C has no arbitrary precision: unsigned __int128 (a compiler extension) fits 2^100; printing must be hand-written */
static void print_u128(unsigned __int128 v) {
    char buf[48];
    int i = 0;
    if (v == 0) { putchar('0'); return; }
    while (v > 0) {
        buf[i++] = (char)('0' + (int)(v % 10));
        v /= 10;
    }
    while (i > 0) putchar(buf[--i]);
}

/* Hand-written fast modular exponentiation (mirrors bcpowmod / Python pow(a, b, m)) */
static unsigned __int128 mod_pow(unsigned __int128 base, unsigned exp, unsigned __int128 m) {
    unsigned __int128 result = 1;
    base %= m;
    while (exp > 0) {
        if (exp % 2 == 1) result = result * base % m;
        base = base * base % m;
        exp /= 2;
    }
    return result;
}

int main(void) {
    /* Powers of 2 are bit shifts (safe only while the exponent < 128) */
    print_u128(((unsigned __int128)1) << 64);
    putchar('\n');
    print_u128(((unsigned __int128)1) << 100);
    putchar('\n');
    /* 64-bit overflow for contrast: unsigned shifts wrap silently, never yielding a true 2^64 */
    printf("%llu\n", (unsigned long long)1 << 63);
    printf("%llu\n", (unsigned long long)mod_pow(2, 100, 97));
    return 0;
}
Output
18446744073709551616
1267650600228229401496703205376
9223372036854775808
16

C++ C++26

C++26
#include <iostream>
#include <string>

// C++ has no arbitrary precision: unsigned __int128 (a compiler extension) fits 2^100; printing needs a hand-written to-string
static std::string to_string_u128(unsigned __int128 v) {
    if (v == 0) return "0";
    std::string s;
    while (v > 0) {
        s += static_cast<char>('0' + static_cast<int>(v % 10));
        v /= 10;
    }
    return std::string(s.rbegin(), s.rend());
}

// Hand-written fast modular exponentiation
static unsigned __int128 mod_pow(unsigned __int128 base, unsigned exp, unsigned __int128 m) {
    unsigned __int128 result = 1;
    base %= m;
    while (exp > 0) {
        if (exp % 2 == 1) result = result * base % m;
        base = base * base % m;
        exp /= 2;
    }
    return result;
}

int main() {
    std::cout << to_string_u128(((unsigned __int128)1) << 64) << "\n";
    std::cout << to_string_u128(((unsigned __int128)1) << 100) << "\n";
    // 64-bit overflow for contrast: unsigned shifts wrap silently
    std::cout << ((unsigned long long)1 << 63) << "\n";
    std::cout << static_cast<unsigned long long>(mod_pow(2, 100, 97)) << "\n";
    return 0;
}
Output
18446744073709551616
1267650600228229401496703205376
9223372036854775808
16

C# v11.0.100

v11.0.100
using System.Numerics;

// BigInteger.Pow: arbitrary-precision powers; long shifts wrap silently in unchecked context
Console.WriteLine(BigInteger.Pow(2, 64));
Console.WriteLine(BigInteger.Pow(2, 100));
Console.WriteLine(unchecked(1L << 63));
// ModPow: modular exponentiation in one call
Console.WriteLine(BigInteger.ModPow(2, 100, 97));
Output
18446744073709551616
1267650600228229401496703205376
-9223372036854775808
16

Go v1.27.4

v1.27.4
package main

import (
	"fmt"
	"math/big"
)

func main() {
	// big.Int.Exp: arbitrary-precision powers; a nil 3rd argument means a plain power, non-nil means modular
	base := big.NewInt(2)
	fmt.Println(new(big.Int).Exp(base, big.NewInt(64), nil))
	fmt.Println(new(big.Int).Exp(base, big.NewInt(100), nil))
	// int64 shifts: constant expressions report an overflow error at compile time, variable math wraps silently at runtime
	var one int64 = 1
	fmt.Println(one << 63)
	fmt.Println(new(big.Int).Exp(base, big.NewInt(100), big.NewInt(97)))
}
Output
18446744073709551616
1267650600228229401496703205376
-9223372036854775808
16

Java v27.0.1

v27.0.1
import java.math.BigInteger;

public class Main {
    public static void main(String[] args) {
        // BigInteger.pow: arbitrary-precision powers (the exponent is an int)
        System.out.println(BigInteger.TWO.pow(64));
        System.out.println(BigInteger.TWO.pow(100));
        // long overflow: silent wraparound (2^63 lands on Long.MIN_VALUE)
        System.out.println((long) 1 << 63);
        // modPow: modular exponentiation in one call
        System.out.println(BigInteger.TWO.modPow(BigInteger.valueOf(100), BigInteger.valueOf(97)));
    }
}
Output
18446744073709551616
1267650600228229401496703205376
-9223372036854775808
16

JavaScript v26.13.1

v26.13.1
// BigInt **: arbitrary precision; Number ** loses precision from 2^63 (a 53-bit mantissa)
console.log((2n ** 64n).toString());
console.log((2n ** 100n).toString());
console.log(2 ** 63);
// No built-in modular exponentiation: hand-written fast pow
function modPow(base, exp, m) {
  let result = 1n;
  base %= m;
  while (exp > 0n) {
    if (exp % 2n === 1n) result = (result * base) % m;
    base = (base * base) % m;
    exp /= 2n;
  }
  return result;
}
console.log(modPow(2n, 100n, 97n).toString());
Output
18446744073709551616
1267650600228229401496703205376
9223372036854776000
16

PHP v8.6.0

v8.6.0
// bcpow: arbitrary-precision powers — 2^64 is far beyond the int cap (~9.2e18), returned exactly as a string
echo bcpow("2", "64"), "\n";
echo bcpow("2", "100"), "\n";
// For contrast: int powers overflow into float from 2^63 — scientific notation and lost precision
echo 2 ** 63, "\n";
// Since PHP 8, a fractional exponent throws ValueError
try {
    bcpow("2", "0.5");
} catch (ValueError) {
    echo "ValueError caught";
}
Output
18446744073709551616
1267650600228229401496703205376
9.2233720368548E+18
ValueError caught

Python v3.15.2

v3.15.2
# ** and pow: integer powers are natively arbitrary-precision (mirroring bcpow); only float powers lose precision
print(2 ** 64)
print(2 ** 100)
print(2.0 ** 63)
# pow(base, exp, mod): modular exponentiation in one call (a cryptography staple)
print(pow(2, 100, 97))
Output
18446744073709551616
1267650600228229401496703205376
9.223372036854776e+18
16

Rust v1.99.1

v1.99.1
// No arbitrary precision in the standard library: u128 caps at 2^128-1, which fits 2^100 (bigger needs the third-party num-bigint crate)
fn main() {
    println!("{}", 1u128 << 64);
    println!("{}", 1u128 << 100);
    // u64 overflow for contrast: unsigned shifts wrap silently
    println!("{}", 1u64 << 63);
    // Modular exponentiation: no built-in mod_pow, hand-written fast pow
    fn mod_pow(mut base: u128, mut exp: u32, m: u128) -> u128 {
        let mut result = 1u128;
        base %= m;
        while exp > 0 {
            if exp % 2 == 1 {
                result = result * base % m;
            }
            base = base * base % m;
            exp /= 2;
        }
        result
    }
    println!("{}", mod_pow(2, 100, 97));
}
Output
18446744073709551616
1267650600228229401496703205376
9223372036854775808
16

TypeScript v26.13.1

v26.13.1
// BigInt **: arbitrary precision; Number ** loses precision from 2^63 (a 53-bit mantissa)
const p64: bigint = 2n ** 64n;
const p100: bigint = 2n ** 100n;
console.log(p64.toString());
console.log(p100.toString());
console.log(2 ** 63);
// No built-in modular exponentiation: hand-written fast pow
function modPow(base: bigint, exp: bigint, m: bigint): bigint {
  let result = 1n;
  base %= m;
  while (exp > 0n) {
    if (exp % 2n === 1n) result = (result * base) % m;
    base = (base * base) % m;
    exp /= 2n;
  }
  return result;
}
console.log(modPow(2n, 100n, 97n).toString());
Output
18446744073709551616
1267650600228229401496703205376
9223372036854776000
16

Language differences

C: no arbitrary precision — unsigned __int128 (a compiler extension) caps at 2^128-1, so powers of two are shifts (exponent < 128); bigger exponents/bases need GMP (mpz_pow_ui, mpz_powm for modular exponentiation); u64 shifts wrap silently on overflow.

C++: same as C; boost::multiprecision::cpp_int (with a pow free function) or GMP for real work.

C#: BigInteger.Pow(value, int exponent) is arbitrary precision; BigInteger.ModPow does modular exponentiation in one step; long overflow wraps silently under unchecked (the compiler only errors on constant overflow by default).

Go: big.Int.Exp(base, exp, nil) — a nil third argument means plain exponentiation, non-nil means modular (the exponent itself can be a big.Int); note int64(1) << 63 as a constant expression is a compile-time overflow error — store it in a variable first and the shift wraps silently at runtime (yielding -2^63).

Java: BigInteger.pow(int) is arbitrary precision, modPow for modular exponentiation; long overflow wraps silently (2^63 lands on Long.MIN_VALUE).

JavaScript: Number's ** loses precision from 2^63 up (53-bit float mantissa; 2 ** 63 prints 9223372036854776000, off by 808 from the true value); BigInt's ** is arbitrary precision; an invalid exponent (negative/non-integer) throws RangeError; no built-in modular exponentiation — hand-write fast exponentiation.

PHP: bcpow takes and returns strings with arbitrary precision; contrast 2 ** 63 overflowing to float (printed as 9.2233720368548E+18, scientific notation with precision loss); since 8.0 a fractional exponent throws ValueError (fractional powers are no longer supported — use bcsqrt for roots); use bcpowmod for modular exponentiation (cryptographic code must use it: power-then-mod would create astronomically large intermediates).

Python: /pow integer exponentiation is arbitrary precision natively (a negative exponent yields a float fraction); pow(base, exp, mod) does modular exponentiation in one step with fast exponentiation inside; 2.0 63 is the float version (9.223372036854776e+18).

Rust: no arbitrary precision in std — powers of two within u128 are shifts (1u128 << 100); beyond that the num-bigint crate (pow, modpow); u64 shifts wrap silently on overflow (debug builds panic on arithmetic overflow but shifts are unchecked).

TypeScript: same as JavaScript.