title: C/C++: Bitwise operators on dynamically allocated memory
body: <p>In C/C++, is there an easy way to apply bitwise operators (specifically left/right shifts) to dynamically allocated memory?</p>
  
  <p>For example, let's say I did this:</p>
  
  <pre><code>unsigned char * bytes=new unsigned char[3];
  bytes[0]=1;
  bytes[1]=1;
  bytes[2]=1;
  </code></pre>
  
  <p><strong>I would like a way to do this:</strong></p>
  
  <pre><code>bytes&gt;&gt;=2;
  </code></pre>
  
  <p>(then the 'bytes' would have the following values):</p>
  
  <pre><code>bytes[0]==0
  bytes[1]==64
  bytes[2]==64
  </code></pre>
  
  <p><strong>Why the values should be that way:</strong></p>
  
  <p>After allocation, the bytes look like this:</p>
  
  <pre><code>[00000001][00000001][00000001]
  </code></pre>
  
  <p>But I'm looking to treat the bytes as one long string of bits, like this:</p>
  
  <pre><code>[000000010000000100000001]
  </code></pre>
  
  <p>A right shift by two would cause the bits to look like this:</p>
  
  <pre><code>[000000000100000001000000]
  </code></pre>
  
  <p>Which finally looks like this when separated back into the 3 bytes (thus the 0, 64, 64):</p>
  
  <pre><code>[00000000][01000000][01000000]
  </code></pre>
  
  <p><strong>Any ideas?</strong> Should I maybe make a struct/class and overload the appropriate operators? Edit: If so, any tips on how to proceed? Note: I'm looking for a way to implement this myself (with some guidance) as a learning experience.</p>