In this post we will discuss few related problems that can be solved without using multiplication and division operators.

##### Problem 1. Multiply a given integer with 15 without using multiplication and division operators.

The idea is to use left shift Bitwise operator to solve this problem. Let the given integer be n. We know that 15n can be rewritten as

1 |
15n = (16n - n) = (n << 4) - n |

We can extend this solution to any number of the form 2^i – 1 where i >= 1 and we can use below expression:

1 |
(n << i) - n |

###### Another approach:

15n can also be rewritten as –

1 |
15n = 8n + 4n + 2n + n = (n << 3) + (n << 2) + (n << 1) + n |

##### Problem 2. Multiply a given even integer with 7.5 without using multiplication and division operators.

We know that 7.5n can also be rewritten as 15n/2. We can easily use expressions used in problem 1 to solve this problem as well.

1 |
7.5n = 15n/2 = ((n << 4) - n) >> 1 |

OR

1 |
7.5n = 15n/2 = ((n << 3) + (n << 2) + (n << 1) + n ) >> 1 |

###### Another approach:

7.5n can also be rewritten as –

1 |
7.5n = 8n - n/2 = ((n << 3) - (n >> 1)) |

Please note that if n is odd, above expression will return ceil of the result. Instead, we can use below expression to return floor

1 |
(n << 3) - (n >> 1) - (n & 1); |

Please note that above solutions are prone to overflow as we’re using left shift operator.

##### Problem 3. Calculate value of 15n/16 without using multiplication and division operators.

Like problem 2, we can easily use expressions used in problem 1 to solve this problem as well.

1 |
15n/16 = (n << 4) - n) >> 4 |

OR

1 |
15n/16 = ((n << 3) + (n << 2) + (n << 1) + n) >> 4 |

###### Another approach:

15n/16 can also be rewritten as –

1 |
15n/16 = n - n/16 = n - (n >> 4) |

Note that first two expressions are equivalent to (15*n)/16 but last expression is equivalent to 15*(n/16). So, results can differ in both.

**Thanks for reading.**

Please use our online compiler to post code in comments. To contribute, get in touch with us.

Like us? Please spread the word and help us grow. Happy coding 🙂

## Leave a Reply