title: C++ - How to use bitfields
body: <p>So I recently came across something like this</p>
  
  <pre><code>unsigned char ch : 7;
  </code></pre>
  
  <p>inside a struct. I read about it a little. Apparently these are called bit fields. They are used to set the width of data that a char can receive.
  But how do we use these things. For example, I know that we can set variable ch to be a byte</p>
  
  <pre><code>unsigned char ch = 0x61;
  cout &lt;&lt; ch &lt;&lt; endl;
  </code></pre>
  
  <p>This will output</p>
  
  <pre><code>a
  </code></pre>
  
  <p>However, what do we do with the bitfields?</p>
  
  <pre><code>unsigned char ch : 7;
  ch = 0x61;                //This doesn't work for some reason
  
  unsigned char ch : 7;
  unsigned char ch = 0x61/  //Neither does this.
  </code></pre>
  
  <p>Thanks for the help</p>