Translate

2015年12月18日 星期五

[Computer Science] Fast Integer Division and Modulus

Sometimes, fast integer division or modulus is required for high speed processing.
Division is normally the most uncomfortable operation for general processors.
Thanks to the math, it is able to convert divisions into bit shift operation by a trick.
Below is a proof of the trick, the idea is inspired from the glorious "Hacker's Delight" 


Since Modulus is a set of operations of division and multiplication like below

a%b = a - (a/b)*b

So, modulus can also take advantages from above trick

沒有留言:

張貼留言