title: Accessing the bits in char through a bitfield
body: <p>I want to access the bits in a <code>char</code> individually.  There are several questions and answers on this topic here on SO, but they all suggest to use boolean mathematics.  However, for my use it would be more convenient if I could simply name the bits separately.  So I was thinking of just accessing the <code>char</code> through a bitfield, like so</p>
  
  <pre><code>#include &lt;stdbool.h&gt;
  #include &lt;stdio.h&gt;
  
  typedef struct {
      bool _1 : 1, _2 : 1, _3 : 1, _4 : 1, _5 : 1, _6 : 1, _7 : 1, _8 : 1;
  } bits;
  
  int main() {
      char c = 0;
      bits *b = (bits *)&amp;c;
      b-&gt;_3 = 1;
      printf("%s\n", c &amp; 0x4 ? "true" : "false");
  }
  </code></pre>
  
  <p>This compiles without errors or warnings with <code>gcc -Wall -Wextra -Wpedantic test.c</code>.  When running the resulting executable with <code>valgrind</code> it reports no memory faults.  The assembly generated for the <code>b-&gt;_3 = 1;</code> assignment is <code>or eax, 4</code> which is sound.  </p>
  
  <p><strong>Questions</strong></p>
  
  <ul>
  <li>Is this defined behaviour in C?</li>
  <li>Is this defined behaviour in C++?</li>
  </ul>
  
  <p>N.B.: I'm aware that it might cause trouble for mixed endianness but I only have little endian.</p>