title: Sizes of bit fields and unions in C
body: <p>I have the following code:</p>
  
  <pre><code>#pragma pack(push, 1)
  typedef struct __attribute__((packed)){
      uint64_t msg: 48;
      uint16_t crc: 12;
      int : 0;
  } data_s;
  #pragma pack(pop)
  
  typedef union {
      uint64_t tot;
      data_s split;
  } data_t;
  
  int main() {
      data_t data;
      printf(
          "Sizes are: union:%d,struct:%d,uint64_t:%d\n",
          sizeof(data),
          sizeof(data.split),
          sizeof(data.tot)
      );
      return 0;
  }
  </code></pre>
  
  <p>The output I get is <code>Sizes are: union:16,struct:10,uint64_t:8</code>.</p>
  
  <p>Here I have two issues, </p>
  
  <ol>
  <li><p>Even though I'm using bit fields and trying to pack it, I am getting 10 bytes even though the number of bits is less than 64(48+12=60) and can be packed into 8 bytes.</p></li>
  <li><p>Even though the maximum size of the two members of the union is 10, why is its size 16?</p></li>
  </ol>
  
  <p>Also how do I pack the bits into 8 bytes?</p>