Powers with integer exponents are quite common in programming tasks. In general, powers may be seen as a faster way to do multiplications. For instance, if we want to multiply the number 5 to itself 8 times, we can write:
5 * 5 * 5 * 5 * 5 * 5 *5 * 5
This process is either tedious, and slow. But, we can use powers:
5 ^ 8 = 5 * 5 * 5 * 5 * 5 * 5 * 5 *5
which means: multiply 5 by itselft 8 times.
Let’s now introduce some formal definitions.
Suppose we have a real number a. Suppose we also have an integer number n greater than 1. The power of a with exponent n is the product:
a^n = a * a * a .... * a (n times)
If we take any a which is real, we have:
a^1 = a
So, a^2 = a*a, a^3 = a*a*a and so on.
And if we take any real a different from zero:
a^0 = 1
We can’t take 0 in this last example, as the term 0 ^ 0 has no meaning.
Now, suppose we have a negative exponent. If a is a real number different than 0, and n is an integer number, then:
a^(-n) = 1 / (a^n)
So, a^(-n) is just the reciprocal of a^n. That means, we can just evaluate it starting from a^n.
The following (boring!) properties are useful for calculations:
a^n * a^m = a^(n+m), with a real a^n / (a^m) = a^(n-m), with a real and not zero (a^n)^m = a ^ (n * m), with a real a ^ n * b ^ n = (a * b) ^ n, with a and b real (a ^ n) / (b ^ n) = (a / b) ^ n, with a real, b real but not 0
Please note that these properties allow us to convert formulas on a form which is faster to compute. For instance, it’s faster to compute only a power and making a sum than computing two powers and making a product. So, a^n * a^m is much slower to compute than the equivalent expression a^(n+m). So, when programming it is important to simplify formulas using these techniques, expecially if we have to make calculations inside a loop.
Of course, powers with integer exponent are more than useful when dealing with binary numbers. An 8 bit number is made up of 8 digits, and each digit can be either 0 or 1. How can we convert a number from binary to decimal?
On an 8 bit number, bits are numbered as follows:
7 6 5 4 3 2 1 0 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
A [ ] can contain a binary digit.
Each bit number is just an exponent. And since we have binary numbers, the base is 2. So, by evaluating each power (using the matching exponent), we can associate a decimal number to each binary digit:
128 64 32 16 8 4 2 1 [ ] [ ] [ ] [ ] [ ] [ ] [ ] [ ]
In facts, 2 ^ 7 = 128, 2 ^ 6 = 64, 2^5 = 32 and so on.
Now, if we want to convert an 8 bit number from binary to decimal, we just need to add the matching decimal values of the binary digits which are equal to 1. So, the binary number 01110011 will be:
128 64 32 16 8 4 2 1        
64 + 32 + 16 + 2+ 1 = 115.
So, 01110011 base 2 equals 115 base 10.