Translate

顯示具有 Compter Science 標籤的文章。 顯示所有文章
顯示具有 Compter Science 標籤的文章。 顯示所有文章

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