Given a integer x, write a function that multiplies x with 3.5 and returns the integer result. You are not allowed to use %, /, *.
1. We can get x*3.5 by adding 2*x, x and x/2. To calculate 2*x, left shift x by 1 and to calculate x/2, right shift x by 2.
2. Another way of doing this could be (8*x – x)/2
Read full article from Multiply a given Integer with 3.5 | GeeksforGeeks
1. We can get x*3.5 by adding 2*x, x and x/2. To calculate 2*x, left shift x by 1 and to calculate x/2, right shift x by 2.
int
multiplyWith3Point5(
int
x)
{
return
(x<<1) + x + (x>>1);
}
int
multiplyWith3Point5(
int
x)
{
return
((x<<3) - x)>>1;
}
No comments:
Post a Comment