C - BitArray - Set single bit of uint64_t
<p>I'm currently working on a project, in which I need bit sets. I'm using an array of <code>uint64_t</code>'s for the bitset.</p>

<p>My current problem is, that whenever I want to set or check a bit I need to do an operation like this:</p>

<pre><code>uint64_t index = 42;
bArr[index/64] |= (((uint64_t)1)&lt;&lt;(index%64)); 
</code></pre>

<p>I can rewrite the division and modulo with some clever <em>and</em> and <em>bitshift</em> operations as well, but I'm concerned about the cast of <code>1</code>. I need this cast, since otherwise the <code>1</code> is seen as a 32-bit unit. As seen in this example - you get wrong output without a cast:</p>

<pre><code>uint64_t bArr[4];                           // 256 bits 
bArr[0] = bArr[1] = bArr[2] = bArr[3] = 0;  // Set to 0

uint64_t i = 255;
bArr[i/64] = (bArr[i/64] | (((uint64_t)1)&lt;&lt;(i%64))); 

uint32_t i2;
for (i2 = 0; i2 &lt; 256; i2++) {
  if ((bArr[i2/64] &amp; (((uint64_t)1)&lt;&lt;(i2%64))) != 0) {
    printf("bArray[%" PRIu32 "] = 1\n", i2);
  }
}
</code></pre>

<p>Can I get around this cast in a clever way? I was thinking that the performance is probably suffering from a cast at <em>every</em> read/write... </p>