Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If don't care of performance, your code is absolutely OK. Otherwise you can do something different.</p> <p>For example Erlang supports Integers of arbitrary size:</p> <pre><code>binary_and(A, B) -&gt; Size = bit_size(A), &lt;&lt;X:Size&gt;&gt; = A, &lt;&lt;Y:Size&gt;&gt; = B, &lt;&lt;(X band Y):Size&gt;&gt;. </code></pre> <p>Or you can handcraft your own binary zip routine:</p> <pre><code>binary_and(A,B) -&gt; binary_and(A, B, &lt;&lt;&gt;&gt;). binary_and(&lt;&lt;A:8, RestA/bytes&gt;&gt;, &lt;&lt;B:8, RestB/bytes&gt;&gt;, Acc) -&gt; binary_add(RestA, RestB, &lt;&lt;Acc/bytes, (A band B):8&gt;&gt;); binary_and(&lt;&lt;&gt;&gt;, &lt;&lt;&gt;&gt;, Result) -&gt; Result. </code></pre> <p>Or optimized version:</p> <pre><code>binary_and(A,B) -&gt; binary_and(A, B, &lt;&lt;&gt;&gt;). binary_and(&lt;&lt;A:64, RestA/bytes&gt;&gt;, &lt;&lt;B:64, RestB/bytes&gt;&gt;, Acc) -&gt; binary_add(RestA, RestB, &lt;&lt;Acc/bytes, (A band B):64&gt;&gt;); binary_and(&lt;&lt;A:8, RestA/bytes&gt;&gt;, &lt;&lt;B:8, RestB/bytes&gt;&gt;, Acc) -&gt; binary_add(RestA, RestB, &lt;&lt;Acc/bytes, (A band B):8&gt;&gt;); binary_and(&lt;&lt;&gt;&gt;, &lt;&lt;&gt;&gt;, Result) -&gt; Result. </code></pre> <p>or more sophisticated</p> <pre><code>binary_and(A,B) -&gt; binary_and({A, B}, 0, &lt;&lt;&gt;&gt;). binary_and(Bins, Index, Acc) -&gt; case Bins of {&lt;&lt;_:Index/bytes, A:64, _/bytes&gt;&gt;, &lt;&lt;_:Index/bytes, B:64, _/bytes&gt;&gt;} -&gt; binary_add(Bins, Index+8, &lt;&lt;Acc/bytes, (A band B):64&gt;&gt;); {&lt;&lt;_:Index/bytes, A:8, _/bytes&gt;&gt;, &lt;&lt;_:Index/bytes, B:8, _/bytes&gt;&gt;} -&gt; binary_add(Bins, Index+1, &lt;&lt;Acc/bytes, (A band B):8&gt;&gt;); {&lt;&lt;_:Index/bytes&gt;&gt;, &lt;&lt;_:Index/bytes&gt;&gt;} -&gt; Acc end. </code></pre> <p>Anyway you have to measure if you are really interested in performance. May be the first one is the fastest for your purposes.</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