exponentiation

The most efficient way to implement an integer based power function pow(int, int)

天涯浪子 提交于 2019-11-26 03:13:12
问题 What is the most efficient way given to raise an integer to the power of another integer in C? // 2^3 pow(2,3) == 8 // 5^5 pow(5,5) == 3125 回答1: Exponentiation by squaring. int ipow(int base, int exp) { int result = 1; for (;;) { if (exp & 1) result *= base; exp >>= 1; if (!exp) break; base *= base; } return result; } This is the standard method for doing modular exponentiation for huge numbers in asymmetric cryptography. 回答2: Note that exponentiation by squaring is not the most optimal

Store and work with Big numbers in C

北城余情 提交于 2019-11-26 02:15:03
问题 I need help working with very big numbers. According to Windows calc, the exponent 174^55 = 1.6990597648061509725749329578093e+123 How would I store this using C (c99 standard)? int main(){ long long int x = 174^55; //result is 153 printf(\"%lld\\n\", x); } 回答1: Normal types in C can usually only store up to 64 bits, so you'll have to store big numbers in an array, for example, and write mathematical operations yourself. But you shouldn't reinvent the wheel here - you could try the GNU

What does the ^ operator do in Java?

a 夏天 提交于 2019-11-25 22:36:32
问题 What function does the ^ (caret) operator serve in Java? When I try this: int a = 5^n; ...it gives me: for n = 5, returns 0 for n = 4, returns 1 for n = 6, returns 3 ...so I guess it doesn\'t perform exponentiation. But what is it then? 回答1: The ^ operator in Java ^ in Java is the exclusive-or ("xor") operator. Let's take 5^6 as example: (decimal) (binary) 5 = 101 6 = 110 ------------------ xor 3 = 011 This the truth table for bitwise (JLS 15.22.1) and logical (JLS 15.22.2) xor: ^ | 0 1 ^ | F