Samples
1. Give an effective way to multiply a number by 7.
As will all trick questions, just a little clarification is needed. In this question what type is the number? Is it an integer or a floating point value?
If the number is an integer, then the most effective way is to use the following formula (n is the input number):
(n<<3) - n = 7 * n
Clarify if you need to write a routine, a macro or just a few lines of code and use it.
If the number were a floating point then there is probably no faster way than 7*n. Although, doing something like this might be faster on certain architectures:
double m = n + n;
double result = m + m + m + n;
The idea is that on some platforms 4 additions might be faster than one multiplication. All depends strictly on the timings of the operations of the running platform.
2. Test if a number is a power of 2.
The first and probably the simplest way to do this is to divide the number by 2 until the remainder equals 1. But this way is very cumbersome, requires a loop and for big numbers there will be many iterations.
There is a better way of doing this and it requires the understanding of binary numbers and boolean algebra.
Come back later for the solution!