How to eliminate a bitwise cycle and process the byte as a whole?
<p>I'm trying to make a function more efficient. This function has 2 cycles and I was said the inner one could be eliminated.</p>

<p>Here's the code:</p>

<pre><code>if (bits === 1 &amp;&amp; colors === 1) {
      var bitMask = (1 &lt;&lt; colors) - 1;
      for (i = 0; i &lt; rowBytes; ++i) {
        var c = rawBytes[i] ^ inbuf;
        for (var b = bitMask &lt;&lt; (8 - colors - colors); b &gt; 0; b &gt;&gt;= 1) {
          c ^= (c &gt;&gt; colors) &amp; b;
        }
        inbuf = (c &amp; bitMask) &lt;&lt; (8 - colors);
        buffer[pos++] = c;
      }
}
</code></pre>

<p>We are dealing with bytes here, so c has 8 bits.</p>

<p>From what I understand of the inner cycle, the value of c is changed according to this:</p>

<ul>
<li>MSB remains equal</li>
<li>If bit on position n is 1, the new bit on that position is the complement of the previous bit. If it is 0, it is equal to the previous one.</li>
</ul>

<p>For example:</p>

<pre><code>1. 1000 0000 -&gt; 1111 1111
2. 1111 1111 -&gt; 1010 1010
</code></pre>

<p>In the 1. case, since all the bits except the first are 0, the resulted bits are all equal to the first. There's no change on their value.</p>

<p>In the 2. case, the bits are all the opposite of the previous one, because the change is always being activated.</p>

<p>What I want to know is how can I make this kind of operation without the cycle i.e. to the byte as a whole.</p>