Multiplying a number without using * operator

c++, java

Solution

Assuming `float x` or `double x` is defined in the scope. Then I see the following possibilities to multiply it by 7 without using the `*` operator:

In C++, you can use the standard functors (first step: create functor, second step: call functor):

x = std::multiplies<float>()(x, 7.0f);  // if x is a float
x = std::multiplies<double>()(x, 7.0);  // if x is a double

Or only use division (Since the compiler already evaluates `1.0 / 7.0`, this is only one step):

x = x / (1.0f / 7.0f);  // if x is a float
x = x / (1.0  / 7.0);   // if x is a double

Or use the `*=` operator (technically, it's not the `*` operator, but it's only one single step):

x *= 7.0f;  // if x is a float
x *= 7.0;   // if x is a double

Or use addition in the logarithmic scale (this is not to be taken very serious, as well as this requires more than two "steps"):

x = exp(log(x) + log(7.0));

Another option is to use an assembly instruction, but I don't want to write that now, since it's overly complicated.

If `x` is an integer, bit shifting is another option, but not recommended:

x = (x << 3) - x;   // (x * 8) - x

Problem

I was going through a programming class and was asked this tricky question which was left unanswered till the end of the class. Question: How can I multiply any input(Float,int etc) by 7, `without using the` `*` operator in `TWO steps`. If anyone can give me the answer for this question with the explanation , that would be very helpful. With TWO STEPS I mean suppose you are running a loop (i=0;i<7;i++) in that case number of steps will be >2, also TYPE CONVERSION, DIVISION,ADDITION etc ( Counts for steps ).

Original source