3

I learned that if you want to multiplicate any binary number with any binary number you do following:

  1. you seperate the number which is the multiplicator in a binary number which holds only one 1. Example:

00000101 = 00000100, 00000001.

  1. Then you do the leftshift which those new numbers

  2. Then you simple add the results.

So what about division with any number?

I heard you do right shift, but that counts only for division by 2. But I want any number to divide. I am not talking about floating numbers. But how could I divide 25 / 5 in binary?

Please tell me an example, Thanks a lot!

I tried to do after the rightshift a subtraction (so in a way like the multiplication), but it won't work :(

Example for multiplication for any numbers:

00001111 * 00000101 means:

00001111 * 00000100 + 00001111 * 00000001 = 00111100 + 00001111 = 01001011 (result)

tshepang
  • 12,111
  • 21
  • 91
  • 136
  • By shifting bits you can multiply/divide by only powers of 2. For any other multiplier/divisor, you'll have to do it in some other way. BTW, can you please give an example of the multiplication method you mentioned? – Sufian Latif Mar 12 '14 at 10:58
  • Have you seen these [1](http://stackoverflow.com/questions/3850665/how-can-i-use-bit-shifting-to-replace-integer-division) [2](http://stackoverflow.com/questions/2776211/how-can-i-multiply-and-divide-using-only-bit-shifting-and-adding) [3](http://stackoverflow.com/questions/16088086/how-to-divide-integer-by-a-constant-integer-with-right-shift-operators) – rmi Mar 12 '14 at 11:07

2 Answers2

2

It doesn't work for division.

The reason it works for multiplication is because multiplication is distributive over addition

17 * 5
17 * (4 + 1)
(17 * 4) + (17 * 1)

Division is not distributive

17 / 5
17 / (4 + 1)
(17 / 4) + (17 / 1) <== WRONG!!
pmg
  • 106,608
  • 13
  • 126
  • 198
  • would you be so kind and explain how divide 2 binary numbers? e.g. 1111:101 ? I trying to understand but i got just not simple explanation here... :( –  Mar 12 '14 at 12:36
  • @int80 just do it like normal decimal division – phuclv Apr 19 '14 at 09:06
0

It doesn't work that way, only for powers of 2. But, there are certain methods possible for division by an integer other than a power of two. See Hacker's delight - Integer division by constants .

Arjun Sreedharan
  • 11,003
  • 2
  • 26
  • 34