Pregunta

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)

¿Fue útil?

Solución

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!!

Otros consejos

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 .

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top