title: Sieve of Eratosthenes using a bit array
body: <p>I have a bit array <code>prime[]</code>of <code>unsigned int</code>. I wish to implement a Sieve of Eratosthenes using this array, by having each bit represent a number <code>n</code>. That is, given <code>n</code>, the array element that holds the bit that corresponds to <code>n</code> would be <code>prime[n/32]</code> and the specific bit would be in position <code>n%32</code>.  </p>
  
  <p>My <code>testBitIs0(int n)</code> function returns 1 when the number is prime (if its bit == 0), otherwise 0:  </p>
  
  <pre><code>return ( (prime[n/32] &amp; (1 &lt;&lt; (n%32) )) != 0);  
  </code></pre>
  
  <p>My <code>setBit(int n)</code> function simply sets the bit to 1 at the corresponding position:  </p>
  
  <pre><code>int i = n/32;
  int pos = n%32;
  unsigned int flag = 1;
  flag = flag &lt;&lt; pos;
  prime[i] = prime[i] | flag;
  </code></pre>
  
  <p>The issue that I'm having is that when I call <code>setBit</code> with multiples of a prime number, I don't think it sets the bit correctly. When I call <code>setBit</code> with multiples of a prime number (such as 4, 6, 8, etc. for the number 2) the next time I run this line:  </p>
  
  <pre><code>if(testBitIs0(i)) { ... }
  </code></pre>
  
  <p>With <code>i = 4/6/8/etc</code> it will still return 1 when it should return 0.<br>
  Can someone please check my code to make sure I am implementing this correctly? Thanks.</p>