Reputation:
I learned that if you want to multiplicate any binary number with any binary number you do following:
00000101 = 00000100, 00000001.
Then you do the leftshift which those new numbers
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)
Upvotes: 3
Views: 592
Reputation: 108938
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!!
Upvotes: 2
Reputation: 11453
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 .
Upvotes: 0