0

I'm interested how to do this, because I just found out you can do integer multiplication easily, by using left shift operators:

x * 25 = (x << 4) + (x << 3) + x

Where the sum of base 2 powers equals to 25: 2^4+2^3+2^0 = 25

How does x / 25 work out with right shifts?

Edit: I'm not going to replace division and multiplication in my programs by these shift operators in hope of speed boost ;)

Rookie
  • 4,064
  • 6
  • 54
  • 86
  • 1
    you dont need to worry about optimization nowadays because compilers are say smarter. but just for the sake a `>>` is a floor(divide by two). – Koushik Shetty Apr 18 '13 at 16:22
  • @Koushik, i know, i'm just interested in how it can be done. – Rookie Apr 18 '13 at 16:24
  • @CaptainObvlious, yes, i found this http://homepage.cs.uiowa.edu/~jones/bcd/divide.html - dont know why the duplicate posts got removed, they had good links – Rookie Apr 18 '13 at 16:31
  • This answer gives a program that seems like a good shortcut to this complex calculation: http://stackoverflow.com/a/6976383/1036205 – Rookie Apr 18 '13 at 16:46
  • You can look here for a start:http://stackoverflow.com/questions/2776211/how-can-i-multiply-and-divide-using-only-bit-shifting-and-adding This is common practice for fast integer math in assembly. At least it was some years ago, not sure if it is still in use. – Devolus Apr 18 '13 at 16:20

0 Answers0