title: How to replace bits in a bitfield without affecting other bits using C
body: <p>I wanted to replace bit/bits (more than one) in a 32/64 bit data field without affecting other bits. Say for example:</p>
  <p>I have a 64-bit register where bits 5 and 6 can take values 0, 1, 2, and 3.</p>
  <pre class="lang-none prettyprint-override"><code>5:6
  ---
  0 0
  0 1
  1 0
  1 1
  </code></pre>
  <p>Now, when I read the register, I get say value 0x146 (0001 0 10 0 0110). Now I want to change the value at bit position 5 and 6 to 01. (Right now it is 10, which is 2 in decimal, and I want to replace it to 1 e 01) without other bits getting affected and write back the register with only bits 5 and 6 modified (so it becomes 126 after changing).</p>
  <p>I tried doing this:</p>
  <pre><code>reg_data = 0x146
  reg_data |= 1 &lt;&lt; shift   // In this case, 'shift' is 5
  </code></pre>
  <p>If I do this, the value at bit positions 5 and 6 will become 11 (0x3), not 01 (0x1) which I wanted.</p>
  <ul>
  <li>How do I go about doing read, modify, and write?</li>
  <li>How do I replace only certain bit/bits in a 32/64 bit fields without affecting the whole data of the field using C?</li>
  </ul>
  <p>Setting a bit is okay, but more than one bit, I am finding it little difficult.</p>