How can I use bit shifting to replace integer division?

Viewed 21248

I understand how to do it for powers of 2 so that's not my question.

For example, if I want to find 5% of a number using a bit shift instead of an integer divide, how would i calculate that?

So instead of (x * 20 / 19), I could do (x * 100 >> 11). Now this isn't right but it's close and I arrived at it using trial and error. How would I determine the most possible precise shift to use?

7 Answers
Related