Assume my system as 32 bit machine. Considering this if I use long int for n>63 I will get my value as 0. How to solve it?
Computer science. Two to the exponent of n, written as 2n, is the number of ways the bits in a binary word of length n can be arranged. A word, interpreted as an unsigned integer, can represent values from 0 (000...0002) to 2n − 1 (111... 1112) inclusively.
So 2 to the zero power is going to be equal to 1. And, actually, any non-zero number to the 0 power is 1 by that same rationale.
pow() is function to get the power of a number, but we have to use #include<math. h> in c/c++ to use that pow() function. then two numbers are passed. Example – pow(4 , 2); Then we will get the result as 4^2, which is 16.
The '4th power' tells you that you'd multiply the number 2 by itself 4 times. In other words, 2 to the 4th power would be the same as 2 x 2 x 2 x 2. Multiply the 2's together and you'll get 16. So, 2 to the 4th power is 16.
double
is perfectly capable of storing powers of two up to 1023 exactly. Don't let someone tell you that floating point numbers are somehow always inexact. This is a special case where they aren't!
double x = 1.0;
for (int n = 0; n <= 200; ++n)
{
printf("2^%d = %.0f\n", n, x);
x *= 2.0;
}
Some output of the program:
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
...
2^196 = 100433627766186892221372630771322662657637687111424552206336
2^197 = 200867255532373784442745261542645325315275374222849104412672
2^198 = 401734511064747568885490523085290650630550748445698208825344
2^199 = 803469022129495137770981046170581301261101496891396417650688
2^200 = 1606938044258990275541962092341162602522202993782792835301376
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With