title: Casting struct to int in c++
body: <p>I have a structure to represent the 29 bit CAN identifier with bit fields as following. </p>
  
  <pre><code>struct canId
  {
      u8 priority         :3; 
      u8 reserved         :1; 
      u8 dataPage         :1;     
      u8 pduFormat        :8;     
      u8 pduSpecific      :8;     
      u8 sourceAddress    :8;     
  } iD;
  </code></pre>
  
  <p>in my code I want to copy this structure to an integer variable. Something like:</p>
  
  <pre><code>int newId = iD; 
  </code></pre>
  
  <p>However I am not sure if this is correct. Can somebody comment on it? </p>
  
  <p>Edit: I can do this using shift operator on each field and then with bitwise OR to have them in the right place. But that makes the use of structure with bit fields useless in the first place. </p>