Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First solutions for shifting (shift is the shift distance, must not be negative, a is the operand to be shifted and contains also the result when done). The power table is used by all three shift operations.</p> <pre class="lang-c prettyprint-override"><code>// table used for shift operations powtab = { 1, 2, 4, 8, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, -32768 }; // logical shift left if (shift &gt; 15) { a = 0; // if shifting more than 15 bits to the left, value is always zero } else { a *= powtab[shift]; } // logical shift right (unsigned) if (shift &gt; 15) { a = 0; // more than 15, becomes zero } else if (shift &gt; 0) { if (a &lt; 0) { // deal with the sign bit (15) a += -32768; a /= powtab[shift]; a += powtab[15 - shift]; } else { a /= powtab[shift]; } } // arithmetic shift right (signed) if (shift &gt;= 15) { if (a &lt; 0) { a = -1; } else { a = 0; } } else if (shift &gt; 0) { if (a &lt; 0) { // deal with the sign bit a += -32768; a /= powtab[shift]; a -= powtab[15 - shift]; } else { // same as unsigned shift a /= powtab[shift]; } } </code></pre> <p>For AND, OR and XOR i could not come up with a simple solution, so i'll do it with looping over each single bit. There might be a better trick to do this. Pseudocode assumes a and b are input operands, c is the result value, x is the loop counter (each loop must run exactly 16 times):</p> <pre class="lang-c prettyprint-override"><code>// XOR (^) c = 0; for (x = 0; x &lt;= 15; ++x) { c += c; if (a &lt; 0) { if (b &gt;= 0) { c += 1; } } else if (b &lt; 0) { c += 1; } a += a; b += b; } // AND (&amp;) c = 0; for (x = 0; x &lt;= 15; ++x) { c += c; if (a &lt; 0) { if (b &lt; 0) { c += 1; } } a += a; b += b; } // OR (|) c = 0; for (x = 0; x &lt;= 15; ++x) { c += c; if (a &lt; 0) { c += 1; } else if (b &lt; 0) { c += 1; } a += a; b += b; } </code></pre> <p>Thats assuming that all variables are 16 bits and all operations behave as signed (so a&lt;0 actually is true when bit 15 is set).</p> <p>EDIT: i actually tested all possible operand values (-32768 to 32767) for shifts ranging from 0 to 31 for correctness and it works correctly (assuming integer divides). For the AND/OR/XOR code an exhaustive test takes too long on my machine, but since the code for these is pretty simple there should be no edge cases anyway.</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.
    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