title: Inconsistent union struct between Linux and Windows (GCC/MSVC)
body: <p>I have a text file that I have to read and parse. each line contains a HEX value in ASCII format. For example:</p>
  
  <pre><code>0100002c
  0100002c
  80000000
  08000000
  0a000000
  </code></pre>
  
  <p>After conversion to signed 32bit integer, I have to check the bits in the following fashion:</p>
  
  <pre><code>bit #31 =&gt; should result in decimal 0 or 1
  bit #30 to 23 =&gt; should result in decimal 0 to 10
  bit #22 to 0 =&gt; should result in a signed decimal number
  </code></pre>
  
  <p>I assign the raw int32 to the following struct/union (I only set the <code>raw</code> part):</p>
  
  <pre><code>typedef struct DATA{
      union {
          int32_t raw;
          struct {
              int32_t data:24;
              uint8_t operation:7;
              uint8_t binop:1;
          };
      } members;
  } data_t;
  </code></pre>
  
  <p>Now the problem is, on a Linux machine and compiling with GCC (I tried 4.8 and 5.4) I get the correct results using the following function:</p>
  
  <pre><code>void vm_data_debug(data_t* const inst, int const num) {
      printf("DEBUG DATA #%d =&gt; "
                     " RAW: %-12d"
                     " BINCODE: %-1d"
                     "\tOPCODE: %-1d"
                     "\tDATA: %-10d", num, inst-&gt;members.raw,
             inst-&gt;members.binop, inst-&gt;members.operation , inst-&gt;members.data);
  
      printf("\tBITS: ");
      vm_data_print_raw_bits(sizeof inst-&gt;members.raw, &amp;inst-&gt;members.raw);
  }
  </code></pre>
  
  <p>And here is the result of the example ASCII on the top of the question on a Linux machine, fine and dandy!</p>
  
  <pre><code>DEBUG DATA #0 =&gt;  RAW: 16777260     BINCODE: 0  OPCODE: 1   DATA: 44            BITS: 00000001000000000000000000101100
  DEBUG DATA #1 =&gt;  RAW: 16777260     BINCODE: 0  OPCODE: 1   DATA: 44            BITS: 00000001000000000000000000101100
  DEBUG DATA #2 =&gt;  RAW: -2147483648  BINCODE: 1  OPCODE: 0   DATA: 0             BITS: 10000000000000000000000000000000
  DEBUG DATA #3 =&gt;  RAW: 134217728    BINCODE: 0  OPCODE: 8   DATA: 0             BITS: 00001000000000000000000000000000
  DEBUG DATA #4 =&gt;  RAW: 167772160    BINCODE: 0  OPCODE: 10  DATA: 0             BITS: 00001010000000000000000000000000
  </code></pre>
  
  <p>Now, using the exact same code on a Windows machine (same machine that I run Linux on) I get a very different result (I have compiled with both MinGW and MSVC2015):</p>
  
  <pre><code>DEBUG DATA #0 =&gt;  RAW: 16777260     BINCODE: 1   OPCODE: 77      DATA: 44                BITS: 00000001000000000000000000101100
  DEBUG DATA #1 =&gt;  RAW: 16777260     BINCODE: 1   OPCODE: 77      DATA: 44                BITS: 00000001000000000000000000101100
  DEBUG DATA #2 =&gt;  RAW: 2147483647   BINCODE: 1   OPCODE: 77      DATA: -1                BITS: 01111111111111111111111111111111
  DEBUG DATA #3 =&gt;  RAW: 134217728    BINCODE: 1   OPCODE: 77      DATA: 0                 BITS: 00001000000000000000000000000000
  DEBUG DATA #4 =&gt;  RAW: 167772160    BINCODE: 1   OPCODE: 77      DATA: 0                 BITS: 00001010000000000000000000000000
  </code></pre>
  
  <p>So the question is, where does this difference come from? What should I do to make it consistent between both Windows and Linux?</p>
  
  <p>I have checked this <a href="https://stackoverflow.com/questions/23987376/why-do-bit-fields-in-c-need-to-be-defined-of-type-unsigned-int-or-signed-int">question</a> but it does not fix it for me, having the union members all signed or unsigned still does not work on Windows.</p>