title: Why does one bitfield struct expression behave different across assignment?
body: <p>Given an expression in C like:</p>
  
  <pre><code>foo = (&lt;an expression&gt;);
  </code></pre>
  
  <p>The compiler will often deign to assign the <code>foo</code> value multiple times. When <code>foo</code> is a register that controls hardware, that can lead to unexpected results. For example, I'm finding that expressions likes</p>
  
  <pre><code>foo = (struct Bar){.field1=13, .field2=42};
  
  foo = Field1Value(13) | Field2Value(42);
  </code></pre>
  
  <p>will generate different sequences of updating foo. The second is generally pretty good about completing the rvalue and then assigning it. But the first often likes to update foo with multiple assignments. I have tried parentheses placement, but the optimizer seems to think otherwise.</p>
  
  <p><em>UPDATE</em></p>
  
  <p>What I wanted to understand is why these 3 statements had different results:</p>
  
  <pre><code>// 1
  GCLK-&gt;CLKCTRL = (GCLK_CLKCTRL_Type){{.ID=GCLK_CLKCTRL_ID_TCC0_TCC1_Val, .GEN=GCLK_CLKCTRL_GEN_GCLK0_Val, .CLKEN=true}};
  // 2
  GCLK-&gt;CLKCTRL.reg = (GCLK_CLKCTRL_Type){{.ID=GCLK_CLKCTRL_ID_TCC0_TCC1_Val, .GEN=GCLK_CLKCTRL_GEN_GCLK0_Val, .CLKEN=true}}.reg;
  // 3
  GCLK_CLKCTRL_Type tmp = {{.ID=GCLK_CLKCTRL_ID_TCC0_TCC1_Val, .GEN=GCLK_CLKCTRL_GEN_GCLK0_Val, .CLKEN=true}};
  GCLK-&gt;CLKCTRL = tmp;
  </code></pre>
  
  <p>The first was problematic. The second 2 work. I wanted to understand why. Obviously, the alternative way to do this kind of thing is with code like:</p>
  
  <pre><code>GCLK-&gt;CLKCTRL.reg = (GCLK_CLKCTRL_ID_TCC0_TCC1 | GCLK_CLKCTRL_GEN_GCLK0 | GCLK_CLKCTRL_CLKEN);
  </code></pre>
  
  <p>The GCLK global is a pointer to this structure def:</p>
  
  <pre><code>typedef struct {
    __IO GCLK_CTRL_Type            CTRL;        /**&lt; \brief Offset: 0x0 (R/W  8) Control */
    __I  GCLK_STATUS_Type          STATUS;      /**&lt; \brief Offset: 0x1 (R/   8) Status */
    __IO GCLK_CLKCTRL_Type         CLKCTRL;     /**&lt; \brief Offset: 0x2 (R/W 16) Generic Clock Control */
    __IO GCLK_GENCTRL_Type         GENCTRL;     /**&lt; \brief Offset: 0x4 (R/W 32) Generic Clock Generator Control */
    __IO GCLK_GENDIV_Type          GENDIV;      /**&lt; \brief Offset: 0x8 (R/W 32) Generic Clock Generator Division */
  } Gclk
  </code></pre>
  
  <p>where the <code>__IO</code> is a macro for <code>volatile</code> among other things.</p>