title: Bit operation of extract flags into 24 bit integer
body: <p>I saw a line in xen's  kernel code (file: xen/include/asm-x86/x86_64/page.h), but cannot understand why they are doing this:</p>
  
  <pre><code>/* Extract flags into 24-bit integer, or turn 24-bit flags into a pte mask. */                                                                      
  #define get_pte_flags(x) (((int)((x) &gt;&gt; 40) &amp; ~0xFFF) | ((int)(x) &amp; 0xFFF))                                                                         
  #define put_pte_flags(x) (((intpte_t)((x) &amp; ~0xFFF) &lt;&lt; 40) | ((x) &amp; 0xFFF))     
  </code></pre>
  
  <p>As to</p>
  
  <pre><code>#define get_pte_flags(x) (((int)((x) &gt;&gt; 40) &amp; ~0xFFF) | ((int)(x) &amp; 0xFFF)) 
  </code></pre>
  
  <p>I understand <code>((int)(x) &amp; 0xFFF)</code> will extract the last 24 bits of x, but why they need the first part <code>((int)((x) &gt;&gt; 40) &amp; ~0xFFF)</code> ? </p>
  
  <p>As to </p>
  
  <pre><code>  #define put_pte_flags(x) (((intpte_t)((x) &amp; ~0xFFF) &lt;&lt; 40) | ((x) &amp; 0xFFF))  
  </code></pre>
  
  <p>I'm lost at the purpose of <code>((intpte_t)((x) &amp; ~0xFFF) &lt;&lt; 40)</code>. It should be 0 in my opinion. Then why do we need it? </p>
  
  <p>Thanks,</p>