title: Can anyone explain this Image Resolution Setup code? Exactly what is going on here
body: <p>I'm trying to understand someone else's code for raw image management.</p>
  <p>Here's what I'm looking at:</p>
  <pre><code>// Resolution has to be a power of 2. 
  // This code finds the lowest RxR resolution which has equal or more pixel than requested
  uint32_t higher = std::max(resolutionX, resolutionY);
  higher--;
  higher |= higher &gt;&gt; 1;
  higher |= higher &gt;&gt; 2;
  higher |= higher &gt;&gt; 4;
  higher |= higher &gt;&gt; 8;
  higher |= higher &gt;&gt; 16;
  higher++;
  internResolution = higher;
  </code></pre>
  <p>I understand that <code>|=</code> is the Bitwise Or operator and that <code>&gt;&gt;</code> is the Shift Bits Right operator.  What I'm not getting is why the original code was designed this way.  I think this pushes <code>Resolution</code> to be a perfect factor of 2, and I'm assuming that is to maintain <a href="https://tanalin.com/en/articles/integer-scaling/" rel="nofollow noreferrer">Pixel perfect integer ratio logic</a>, but this code seems an odd way to get there.</p>
  <p>Can someone explain what is going on here, and perhaps why this is designed this way?  Is this just an abstruse way to get to <a href="https://www.geeksforgeeks.org/smallest-power-of-2-greater-than-or-equal-to-n/" rel="nofollow noreferrer">&quot;Smallest power of 2 greater than or equal to n&quot;</a></p>