Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<pre><code>template &lt;typename T&gt; </code></pre> <p>Does this really need to be parameterized? It would seem that any shift quantity can be stored in a <code>size_t</code>, and converting to <code>size_t</code> implicitly or explicitly would be a good idea before starting the rest of the process.</p> <pre><code>integer operator&lt;&lt;(T shift){ </code></pre> <p>Usually binary <code>operator</code> functions are best implemented as non-member <code>friend</code>s. For example, fewer conversions are considered when producing the <code>this</code> pointer than for generic operands. I'd write <code>friend integer operator&lt;&lt; ( integer lhs, size_t shift )</code>.</p> <pre><code> std::list &lt;uint8_t&gt; out = value; </code></pre> <p>If you use a <code>friend</code> and pass-by-value, then this copy is made implicitly. The compiler also has an easier time eliminating it if you are really just modifying one object, e.g. <code>q = q &lt;&lt; 3</code>.</p> <pre><code> for(unsigned int i = 0; i &lt; (shift &gt;&gt; 3); i++)// get rid of bytes if shift &gt; 8 </code></pre> <p>Be careful in the loop condition. You're applying an operator to a high-level type, which might cause an expensive computation (or even endless recursion if <code>T</code> is <code>integer</code>!</p> <pre><code> shift &amp;= 7; // shift by less than a byte </code></pre> <p>At this point, if <code>shift == 0</code> you are done. Best to insert a conditional <code>return</code> here. Also, now the range is <em>really</em> limited, so assign to a narrower type than <code>T</code>.</p> <pre><code> out.push_front(0); // extra byte for overflow </code></pre> <p>This is only necessary if the byte will be nonzero. Probably best to special-case the first operation and make the <code>push_front</code> conditional, instead of special-casing the last.</p> <p>… Hmm, it looks like the <code>operator&gt;&gt;</code> is being exercised more… skipping to that…</p> <pre><code> for(; i != j; i++){ i++; uint8_t temp = *i &lt;&lt; (8 - shift); i--; *i += temp; } *j &gt;&gt;= shift; </code></pre> <p>Clearly the <code>&gt;&gt;</code> operator is missing from the loop. Try <code>*i = *i &gt;&gt; shift + temp</code>. Also, I don't see how the list ever gets shorter. Is that done in <code>integer::integer( list&lt;…&gt; )</code>?</p> <p>However, I can't really see what produces the behavior in the posted output. The leading zero is probably a result of the unconditional <code>push_front</code> in <code>operator&lt;&lt;</code>, but the pattern I'd expect is <code>c</code>, <code>6</code>, <code>3</code>, <code>18</code>, <code>c</code>, …. You don't seem to be repeating any sequence, but instead jump randomly.</p> <p>Maybe the division code, or the class constructor could provide clues.</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