Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Try:</p> <pre><code>x ^= 0x14; </code></pre> <p>That toggles both bits. It's a little bit unclear in question as you first mention swap and then give a toggle example. Anyway, to swap the bits:</p> <pre><code>x = precomputed_lookup [x]; </code></pre> <p>where precomputed_lookup is a 256 byte array, could be the fastest way, it depends on the memory speed relative to the processor speed. Otherwise, it's:</p> <pre><code>x = (x &amp; ~0x14) | ((x &amp; 0x10) &gt;&gt; 2) | ((x &amp; 0x04) &lt;&lt; 2); </code></pre> <p>EDIT: Some more information about toggling bits.</p> <p>When you xor (<code>^</code>) two integer values together, the xor is performed at the bit level, like this:</p> <pre><code>for each (bit in value 1 and value 2) result bit = value 1 bit xor value 2 bit </code></pre> <p>so that bit 0 of the first value is xor'ed with bit 0 of the second value, bit 1 with bit 1 and so on. The xor operation doesn't affect the other bits in the value. In effect, it's a parallel bit xor on many bits.</p> <p>Looking at the truth table for xor, you will see that xor'ing a bit with the value '1' effectively toggles the bit.</p> <pre><code> a b a^b 0 0 0 0 1 1 1 0 1 1 1 0 </code></pre> <p>So, to toggle bits 1 and 3, write a binary number with a one where you want the bit to toggle and a zero where you want to leave the value unchanged:</p> <pre><code>00001010 </code></pre> <p>convert to hex: 0x0a. You can toggle as many bits as you want:</p> <pre><code>0x39 = 00111001 </code></pre> <p>will toggle bits 0, 3, 4 and 5</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

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