title: how to binary fan out
body: <p>I want to fan out binary data like in the following examples:</p>
  
  <pre><code>8 bit to 16 bit: 
       0 0 0 1   0 0 0 1  (0x11)    becomes 
      0000 0011 0000 0011 (0x0303)
  
  8 bit to 32 bit: 
         0    1    0    0    0    1    1    0 (0x46)       becomes 
      0000 1111 0000 0000 0000 1111 1111 0000 (0x0F000FF0)
  
  16 to 32 bit: 
       0 0 0 1   0 0 1 1   0 1 1 1   1 1 1 1  (0x137F)     becomes 
      0000 0011 0000 1111 0011 1111 1111 1111 (0x030F3FFF)
  </code></pre>
  
  <p>Since this question is tagged with <code>bit-manipulation</code>, I am looking for a compact solution without any loops or additional variables needed. The code should also be platform independent, so inline assembly is not what I want either.</p>
  
  <p>This may be a duplicate question, but I have no idea how to search for it. Most certainly there is a proper name for what I want to do, but I just don't know it.</p>
  
  <p><strong>EDIT:</strong>
  To clarify some of your comments, here is a list of methods the solution should <strong>not</strong> contain:</p>
  
  <ul>
  <li>assembly code (platform dependent)</li>
  <li>'complex' computation like loops (that's why I tagged this question with <code>bit-fiddling</code>)</li>
  <li>additional runtime variables (e.g. helper constants, look up tables, ...)</li>
  </ul>
  
  <p>Since you asked for it, here are some further details on my use case: I'm working on embedded platforms (microcontrollers) and want to mask registers. This specific case is about IO registers, where I have 16 pads for each port and several port registers to configure the pads. Some of these configuration registers use 2 bits per pad, some 4 bits. So if I want to mask individual pads when accessing the registers I need to mask those registers accordingly.</p>
  
  <p>Let's say I have two configuration registers and want to access pads 4 and 8:</p>
  
  <pre><code>uint32_t configreg_2perpad;       // the first configuration register
  uint32_t configreg_4perpad_low;   // lower half of the second configuration register
  uint32_t configreg_4perpad_high;  // higher half of the second configuration register
  
  uint8_t pad4 = 3;                 // we start counting at 0, of course
  uint8_t pad8 = 7;
  uint16_t pad_mask = (1 &lt;&lt; pad4) |
                      (1 &lt;&lt; pad8);  // this is now 0x0088
  </code></pre>
  
  <p>So what I am looking for now is a bit-wise manipulation of <code>pad_mask</code> so I get according masks for the registers, which would be</p>
  
  <pre><code>uint32_t configreg_2perpad_mask = 0x0000C0C0;
  uint32_t configreg_4perpad_low_mask = 0xF000F000;
  uint32_t configreg_4perpad_high_mask = 0x00000000;
  </code></pre>
  
  <p>Obviously, since memory (both RAM and flash) is a very scarce resource on µCs, I would prefer a solution without the need for further static variables like look up tables (waste of RAM) or static functions (waste of flash).</p>