title: What's recommended practice for bitset manipulation?
body: <p>I'm working on a machine simulation program.  I've got a vector of bitsets for main memory, so that I may use a pointer to this vector, pMemory->at(i), to access any specific "word".  I really do prefer the vector-of-bitsets design, and I'm sticking with it (this program is due in... about 6 hours, eek!)</p>
  
  <p>I've been having some trouble trying to figure out how to get bitsets in and out of different locations (simulated registers and other memory locations, etc), so I've read up some on using streams.  I've come up with this:</p>
  
  <pre><code>#include &lt;bitset&gt;
  #include &lt;iostream&gt;
  #include &lt;sstream&gt;
  #include &lt;string&gt;
  
  using namespace std;
  
  int main()
  {
  
  
      /** demonstrating use of stringstream to/from bitset **/
      {
          bitset&lt;12&gt; sourceBits(std::string("011010010100"));
          bitset&lt;12&gt; targetBits(0);
  
          stringstream iBits(stringstream::in | stringstream::out);
  
          iBits &lt;&lt; sourceBits.to_string();
          cout &lt;&lt; targetBits &lt;&lt; endl;
          iBits &gt;&gt; targetBits;
          cout &lt;&lt; targetBits &lt;&lt; endl;
      } //end stringstream to/from bitset
  
      return 0;
  }
  </code></pre>
  
  <p>So, this works, and I can adapt this technique to fit my program.</p>
  
  <p>My questions are, is this a good idea?  Is there something fundamental I'm missing about using the bitset >> and &lt;&lt; operators?  Is it really necessary to do all this manual wrangling?</p>
  
  <p>Also, tangentially, what should I do when copying a 12-bit bitset into a 16-bit bitset?</p>
  
  <p>Thank you, stackoverflow!  This is my first question to this community after <em>much</em> googling.  I appreciate everyone's insights!</p>