title: Bitshifts to obtain remainder
body: <p>I want to know how to obtain the remainder by dividing an integer with another integer (both positive) using bitshift or bitwise operators only. The <code>/</code> operator or <code>%</code> operator should not be used.</p>
  
  <p>For example, for obtaining the remainder when divisor is of the form <code>2^k</code> the following operation yields the remainder.</p>
  
  <p><code>m = Remainder</code></p>
  
  <p><code>n = The number</code></p>
  
  <p><code>d = The divisor</code></p>
  
  <p><code>m = n &amp; ( d - 1 )</code></p>
  
  <p>However this method works only when <code>d</code> is of the form <code>2^k</code> . I want to know a similar method for non-powers of <code>2</code>. I am currently working on a problem from <code>programming challenges</code> and want to employ such a method to reduce program execution time</p>