title: Structures with bitwise data in C++
body: <blockquote>
    <p><strong>Possible Duplicate:</strong><br>
    <a href="https://stackoverflow.com/questions/2468708/converting-bit-field-to-int">Converting Bit Field to int</a>  </p>
  </blockquote>
  
  
  
  <p>I am working on an application, part of which handles 16-bit words that contain a number of 1-bit flags.  I am handling the data using a structure similar to the one shown below: </p>
  
  <pre><code>struct mystruct
  {
     uint16_t Reserved1   :3;
     uint16_t WordErr     :1;
     uint16_t SyncErr     :1;
     uint16_t WordCntErr  :1;
     uint16_t Reserved2   :10;
  };
  </code></pre>
  
  <p>i.e. the structure contains a single 16-bit variable that is handled as a number of smaller (in some cases, 1-bit flag) pieces.  </p>
  
  <p>My question is this, is there a simple way to handle the entire 16-bit word as one value, say, to output it to the console, or a file, or add it to another data structure?  I don't know of any way of doing this besides shifting the individual structure elements and adding them to a temporary uint16_t variable.  It seems that there is probably a simpler way of extracting the entire word, but I can't find any information on how the compiler handles a structure like this.  </p>
  
  <p>EDIT: I suppose this may be obvious but what I am trying to do in a nutshell is be able to access the 1-bit flags individually, as well as use the structure as a single variable of type uint16_t (i.e. unsigned short, 16 bits).  </p>