title: Bit Buffer in C++
body: <p>I'm doing an assignment in which I need to write bit to a buffer and once the buffer has been fill with 8 bits, I would flush. (Huffman compression)</p>
  
  <p>I have a problem assigning/saving the intermediate stages during input. So I did a little test such as</p>
  
  <pre><code>char buf = 0;
  
  cout &lt;&lt; (buf | (1 &lt;&lt; 1));
  buf = (buf | (1 &lt;&lt; 1));
  cout &lt;&lt; buf;
  </code></pre>
  
  <p>but not understand why I can't save the intermediate stage. The output of that would be:</p>
  
  <pre><code>2"happy face"
  </code></pre>
  
  <p>instead of</p>
  
  <pre><code>22
  </code></pre>
  
  <p>Can you please teach me how to save the intermediate stages?</p>
  
  <p>Basically, I would need to write bit by bit to the buffer, and once the buffer is a byte, I will flush it to my output file.</p>