title: Explanation of an algorithm to set, clear and test a single bit
body: <p>Hey, in the Programming Pearls book, there is a source code for setting, clearing and testing a bit of the given index in an array of ints that is actually a set representation.</p>
  
  <p>The code is the following:</p>
  
  <pre><code>#include&lt;stdio.h&gt;
  #define BITSPERWORD 32
  #define SHIFT 5
  #define MASK 0x1F
  #define N 10000000
  
  int a[1+ N/BITSPERWORD];
  
  void set(int i)
  {
      a[i&gt;&gt;SHIFT] |= (1&lt;&lt;(i &amp; MASK));
  }
  
  void clr(int i)
  {
      a[i&gt;&gt;SHIFT] &amp;= ~(1&lt;&lt;(i &amp; MASK));
  }
  
  int test(int i)
  {
      a[i&gt;&gt;SHIFT] &amp; (1&lt;&lt;(i &amp; MASK));
  }
  </code></pre>
  
  <p>Could somebody explain me the reason of the SHIFT and the MASK defines? And what are their purposes in the code?</p>
  
  <p>I've already read the previous related <a href="https://stackoverflow.com/questions/47981/how-do-you-set-clear-and-toggle-a-single-bit-in-c">question</a>.</p>