Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>The assumption that the <code>&amp;</code> operator is always irreversible is incorrect.</p> <p>Yes, in general if you have</p> <pre><code>c = a &amp; b </code></pre> <p>and all you know is the value of <code>c</code>, then you cannot know what values <code>a</code> or <code>b</code> had before hand.</p> <p>However it's very common for <code>&amp;</code> to be used to <em>extract</em> certain bits from a longer value, where those bits were previously combined together with the <code>|</code> operator and where each 'bit field' is independent of every other. The fundamental difference with the generic <code>&amp;</code> or <code>|</code> operators that makes this reversible is that the original bits were <em>all zero</em> beforehand, and the other bits in the word are left unchanged. i.e:</p> <pre><code>0xc0 | 0x03 = 0xc3 // combine two nybbles 0xc3 &amp; 0xf0 = 0xc0 // extract the top nybble 0xc3 &amp; 0x0f = 0x03 // extract the bottom nybble </code></pre> <p>In this case your current function appears to be extracting a 1 bit-per-pixel (monochrome image) and converting it to 32-bit RGBA.</p> <p>You'll need something like:</p> <pre><code>int source_image[]; byte dest_image[]; for (int h = 0; h &lt; height; ++h) { for (int w = 0; w &lt; width; ++w) { int offset = (h * width) + w; if (source_image[offset] == 0xffffffff) { int mask = w % 8; // these two lines convert from one int-per-pixel offset /= 8; // offset to one-bit-per-pixel dest_image[offset] |= (1 &lt;&lt; mask); // only changes _one_ bit } } } </code></pre> <p>NB: assumes the image is a multiple of 8 pixels wide, that the <code>dest_image</code> array was previously all zeroes. I've used <code>%</code> and <code>/</code> in that inner test because it's easier to understand and the compiler <em>should</em> convert to mask / shift itself. Normally I'd do the masking and shifting myself.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload