0

Possible Duplicate:
implement division with bit wise operator
Divide a number by 3 without using *, /, +, -, % operators

I came across this question in an interview. I want to know if there any possibly way to divide a number by 5 without using division operator and if any possible solution exists using bitwise operators only.I figured one out using repeated subtraction till zero approaches. Number can be signed and unsigned. Please suggest any way out without using +,-,/,* and %.

Community
  • 1
  • 1
Jeris
  • 2,355
  • 4
  • 26
  • 38
  • 1
    What format is the "number" ? 2s complement integer ? Floating point ? BCD ? Other ? – Paul R Dec 14 '12 at 12:49
  • Consider the format to be in decimal format – Jeris Dec 14 '12 at 12:53
  • the following question contains solution for deviding by 3. It could help you to find the equivalent for 5 : http://stackoverflow.com/questions/11694546/divide-a-number-by-3-without-using-operators – MOHAMED Dec 14 '12 at 12:57
  • Wait what do you want to do with the remainder? I.e. 14/5 equals what? – Woot4Moo Dec 14 '12 at 12:59
  • [Divisiblity of 5 without using % and / operator](http://stackoverflow.com/q/17113660/995714) – phuclv Sep 09 '15 at 12:24
  • 1
    the other duplicates are for dividing by general cases and by 3. This should be reopened. [MS has explained about the divide by 5 case here](https://blogs.msdn.microsoft.com/devdev/2005/12/12/integer-division-by-constants/) – phuclv Jul 29 '18 at 03:43

3 Answers3

0

My first idea was to just multiply by 0.2 (but I don't know a solution for how to implement that using bitwise operators from the top of my head).

Frerich Raabe
  • 90,689
  • 19
  • 115
  • 207
0

Simply reduce division to subtracting one number from the other until you reach zero :D

int number = 25;
int divisor = 5;
int result = 0;
while((number-divisor)>=0){
  result++;
  number = number - divisor;
}
Cédric Julien
  • 78,516
  • 15
  • 127
  • 132
LuigiEdlCarno
  • 2,410
  • 2
  • 21
  • 37
0

I seem to have found a way out from this link which seems to provide an answer to my question. http://codegambler.wordpress.com/2009/08/11/division-operation-without-using-division-operator/

Soroush Chehresa
  • 5,490
  • 1
  • 14
  • 29
Jeris
  • 2,355
  • 4
  • 26
  • 38
  • 1
    That code uses addition and subtraction though: `quotient+=division(p-q,divisor);` – Kevin Dec 14 '12 at 12:57
  • @Kevin glad you pointed it out. I must have specified a proper algorithm in my question. My mistake. – Jeris Dec 14 '12 at 13:04