title: Given a 32 bit number, what is an efficient way to scale each byte by a certain factor?
body: <p>Given a uint32 number 0x12345678 (e.g., an RGBW color value), how could I <strong>efficiently</strong> and dynamically scale each byte in it (given a scaling factor <code>0 &lt;= f &lt;= 1</code> (or an equivalent integer divisor)?</p>
  
  <p>I know I could do this a longer way (break the number into its components, perhaps via a struct, and loop to manipulate each in turn), but is there a way you to do it faster, without looping? (Static value mapping would be another way, but a dynamic method is preferable.)</p>
  
  <p>Edit: C++ (C ideas are interesting as well), embedded, hundreds or thousands of pixels (not millions). Specifically scaling RGBW leds.</p>
  
  <p>One other thing that came up - this is with gcc, so <a href="https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Type-punning" rel="noreferrer">type punning is allowed</a> (I already use it for similar things - I just wanted to see if there's a better way than that).</p>
  
  <p>Edit again: This is for embedded platforms (microcontrollers). While I'm all for answers that help a wider audience, I purposely asked about this in the context of the language(s) and algorithms rather than optimizations for specific platforms and instruction sets, as platform-specific optimizations can vary if present at all.</p>