<div dir="ltr"><div>This kind of pattern is very common when dealing with bit arrays:</div><div><br></div><div>unsigned long clear_mask(int bit)<br></div><div>{</div><div>    return ~(1UL << bit);</div><div>}</div><div><br></div><div>clang 3.6 at -O3 compiles this in the straightforward way, moving 1 into register, shifting it, then flipping all the bits:</div><div><br></div><div><div><span class="" style="white-space:pre">   </span>movl<span class="" style="white-space:pre">      </span>$1, %eax</div><div><span class="" style="white-space:pre">   </span>movb<span class="" style="white-space:pre">      </span>%dil, %cl</div><div><span class="" style="white-space:pre">  </span>shlq<span class="" style="white-space:pre">      </span>%cl, %rax</div><div><span class="" style="white-space:pre">  </span>notq<span class="" style="white-space:pre">      </span>%rax</div></div><div><br></div><div>gcc 4.8.2, however, is more clever. It moves -2, i.e. ~1, into a register then rotates that into place, saving one instruction:<br><br></div><div><div><span class="" style="white-space:pre">     </span>movl<span class="" style="white-space:pre">      </span>%edi, %ecx</div><div><span class="" style="white-space:pre"> </span>movq<span class="" style="white-space:pre">      </span>$-2, %rax</div><div><span class="" style="white-space:pre">  </span>rolq<span class="" style="white-space:pre">      </span>%cl, %rax</div></div><div><br></div><div><br></div></div>