<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: Three Binary Algorithms</title>
	<atom:link href="http://programmingpraxis.com/2010/01/15/three-binary-algorithms/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2010/01/15/three-binary-algorithms/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Sat, 11 Feb 2012 09:48:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Graham</title>
		<link>http://programmingpraxis.com/2010/01/15/three-binary-algorithms/#comment-3132</link>
		<dc:creator><![CDATA[Graham]]></dc:creator>
		<pubDate>Sun, 12 Jun 2011 14:12:26 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1887#comment-3132</guid>
		<description><![CDATA[Working my way through old exercises; here&#039;s my Python solution (note: the previously posted solution doesn&#039;t look like it quite conquered division):
[sourcecode lang=&quot;python&quot;]
def mul(n, m):
    prod = 0
    while n != 1:
        if n &amp; 1: prod += m
        m &lt;&lt;= 1
        n &gt;&gt;= 1
    return prod + m

def div(n, m):
    t = m
    while t &lt;= n: t &lt;&lt;= 1
    t, q, r = t &gt;&gt; 1, 0, n
    while t &gt;= m:
        q &lt;&lt;= 1
        if t &lt;= r: q, r = q + 1, r - t
        t &gt;&gt;= 1
    return q, r


def gcd(n, m):
    if n == 0 or m == 0: return m + n
    elif not n &amp; 1 and not m &amp; 1: return gcd(n &gt;&gt; 1, m &gt;&gt; 1) &lt;&lt; 1
    elif not n &amp; 1: return gcd(n &gt;&gt; 1, m)
    elif not m &amp; 1: return gcd(n, m &gt;&gt; 1)
    else: return gcd(min(n, m), max(n, m) - min(n, m))
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Working my way through old exercises; here&#8217;s my Python solution (note: the previously posted solution doesn&#8217;t look like it quite conquered division):</p>
<pre class="brush: python;">
def mul(n, m):
    prod = 0
    while n != 1:
        if n &amp; 1: prod += m
        m &lt;&lt;= 1
        n &gt;&gt;= 1
    return prod + m

def div(n, m):
    t = m
    while t &lt;= n: t &lt;&lt;= 1
    t, q, r = t &gt;&gt; 1, 0, n
    while t &gt;= m:
        q &lt;&lt;= 1
        if t &lt;= r: q, r = q + 1, r - t
        t &gt;&gt;= 1
    return q, r

def gcd(n, m):
    if n == 0 or m == 0: return m + n
    elif not n &amp; 1 and not m &amp; 1: return gcd(n &gt;&gt; 1, m &gt;&gt; 1) &lt;&lt; 1
    elif not n &amp; 1: return gcd(n &gt;&gt; 1, m)
    elif not m &amp; 1: return gcd(n, m &gt;&gt; 1)
    else: return gcd(min(n, m), max(n, m) - min(n, m))
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Mike</title>
		<link>http://programmingpraxis.com/2010/01/15/three-binary-algorithms/#comment-1147</link>
		<dc:creator><![CDATA[Mike]]></dc:creator>
		<pubDate>Sat, 10 Apr 2010 00:42:22 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1887#comment-1147</guid>
		<description><![CDATA[Python:

[sourcecode lang=&quot;python&quot;]

def binmul(p, s):
    prod = 0
    while p&gt;=1:
        if p&amp;1:
            prod += s
        p &gt;&gt;= 1
        s &lt;&lt;= 1
    return prod


def bindiv(n, d):
    t = d
    while t &lt; n:
        t &lt;&lt;= 1
    t &gt;&gt;= 1

    q, r = 0, n
    while t &gt;= d:
        q &lt;&lt;= 1
        if t &lt; r:
            q += 1
            r -= t
        t &gt;&gt;= 1
    return q,r


def bingcd(a, b):
    twos = 0

    while a and b:
        if a&amp;1:
            if b&amp;1:
                a,b = (a-b,b) if a&gt;b else (a,b-a)
            else:
                b &gt;&gt;= 1
        elif b&amp;1:
            a &gt;&gt;= 1
        else:
            twos += 1
            a &gt;&gt;= 1
            b &gt;&gt;= 1

    return (a or b) &lt;&lt; twos

[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>Python:</p>
<pre class="brush: python;">

def binmul(p, s):
    prod = 0
    while p&gt;=1:
        if p&amp;1:
            prod += s
        p &gt;&gt;= 1
        s &lt;&lt;= 1
    return prod

def bindiv(n, d):
    t = d
    while t &lt; n:
        t &lt;&lt;= 1
    t &gt;&gt;= 1

    q, r = 0, n
    while t &gt;= d:
        q &lt;&lt;= 1
        if t &lt; r:
            q += 1
            r -= t
        t &gt;&gt;= 1
    return q,r

def bingcd(a, b):
    twos = 0

    while a and b:
        if a&amp;1:
            if b&amp;1:
                a,b = (a-b,b) if a&gt;b else (a,b-a)
            else:
                b &gt;&gt;= 1
        elif b&amp;1:
            a &gt;&gt;= 1
        else:
            twos += 1
            a &gt;&gt;= 1
            b &gt;&gt;= 1

    return (a or b) &lt;&lt; twos
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jebb</title>
		<link>http://programmingpraxis.com/2010/01/15/three-binary-algorithms/#comment-954</link>
		<dc:creator><![CDATA[Jebb]]></dc:creator>
		<pubDate>Sun, 24 Jan 2010 16:11:57 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1887#comment-954</guid>
		<description><![CDATA[And the GCD:
[sourcecode lang=&quot;cpp&quot;]
#define EVEN(A) (!(A &amp; 1))
#define MAX(A, B) (A &gt; B ? A : B)
#define MIN(A, B) (A &gt; B ? B : A)

int gcd(unsigned int m, unsigned int n)
{
    if ((m == 0) &#124;&#124; (n == 0))
        return MAX(m, n);
    if (EVEN(m) &amp;&amp; EVEN(n))
        return (gcd((m &gt;&gt; 1), (n &gt;&gt; 1)) &lt;&lt; 1);
    if (EVEN(m) &#124;&#124; EVEN(n)) { //we&#039;ve just tested for &amp;&amp;, this is now effectively an XOR
        unsigned int odd, even;
        EVEN(m) ? (even = m, odd = n) : (even = n, odd = m);
        return gcd(odd, (even &gt;&gt; 1));
    }
    return gcd(MAX(m, n) - MIN(m, n), MIN(m, n));
}
[/sourcecode]

I managed to get this one spectacularly wrong initially, by underestimating the difference between preprocessor macros and functions: I&#039;d initially left out the brackets around the expression of the MAX and MIN macros, and obviously the operators precedence was playing a nasty trick on me in the gcd(MAX(m,n) - MIN(m,n), MIN(m,n)). I&#039;d like to think it&#039;s a lesson I won&#039;t forget...]]></description>
		<content:encoded><![CDATA[<p>And the GCD:</p>
<pre class="brush: cpp;">
#define EVEN(A) (!(A &amp; 1))
#define MAX(A, B) (A &gt; B ? A : B)
#define MIN(A, B) (A &gt; B ? B : A)

int gcd(unsigned int m, unsigned int n)
{
    if ((m == 0) || (n == 0))
        return MAX(m, n);
    if (EVEN(m) &amp;&amp; EVEN(n))
        return (gcd((m &gt;&gt; 1), (n &gt;&gt; 1)) &lt;&lt; 1);
    if (EVEN(m) || EVEN(n)) { //we've just tested for &amp;&amp;, this is now effectively an XOR
        unsigned int odd, even;
        EVEN(m) ? (even = m, odd = n) : (even = n, odd = m);
        return gcd(odd, (even &gt;&gt; 1));
    }
    return gcd(MAX(m, n) - MIN(m, n), MIN(m, n));
}
</pre>
<p>I managed to get this one spectacularly wrong initially, by underestimating the difference between preprocessor macros and functions: I&#8217;d initially left out the brackets around the expression of the MAX and MIN macros, and obviously the operators precedence was playing a nasty trick on me in the gcd(MAX(m,n) &#8211; MIN(m,n), MIN(m,n)). I&#8217;d like to think it&#8217;s a lesson I won&#8217;t forget&#8230;</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jebb</title>
		<link>http://programmingpraxis.com/2010/01/15/three-binary-algorithms/#comment-953</link>
		<dc:creator><![CDATA[Jebb]]></dc:creator>
		<pubDate>Sun, 24 Jan 2010 16:06:32 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1887#comment-953</guid>
		<description><![CDATA[...sorry about the bit in french in the previous one. Division:

[sourcecode lang=&quot;cpp&quot;]
int div(unsigned int n, unsigned int d)
{
    unsigned int q = 0, t = d, r = n;
    while (t &lt;= n)
        t &lt;&lt;= 1;
    while ((t &gt;&gt;= 1) &gt;= d) {
        if (t &lt;= r) {
            q = 1 + (q &lt;&lt; 1);
            r -= t;
        }
        else
            q &lt;&lt;= 1;
    }
    printf(&quot;quotient %d, remainder %d\n&quot;, q, r);
    return 0;
}[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>&#8230;sorry about the bit in french in the previous one. Division:</p>
<pre class="brush: cpp;">
int div(unsigned int n, unsigned int d)
{
    unsigned int q = 0, t = d, r = n;
    while (t &lt;= n)
        t &lt;&lt;= 1;
    while ((t &gt;&gt;= 1) &gt;= d) {
        if (t &lt;= r) {
            q = 1 + (q &lt;&lt; 1);
            r -= t;
        }
        else
            q &lt;&lt;= 1;
    }
    printf(&quot;quotient %d, remainder %d\n&quot;, q, r);
    return 0;
}</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Jebb</title>
		<link>http://programmingpraxis.com/2010/01/15/three-binary-algorithms/#comment-952</link>
		<dc:creator><![CDATA[Jebb]]></dc:creator>
		<pubDate>Sun, 24 Jan 2010 15:59:07 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1887#comment-952</guid>
		<description><![CDATA[I&#039;ll break this down in three replies, C is just too verbose. I&#039;m fascinated by how compact your Scheme and Haskell solutions always are... I really wanted to have a serious look at Python as a next step at learning to program, but this could make me change my mind.

Anyway, multiplication:

[sourcecode lang=&quot;cpp&quot;]
#define EVEN(A) (!(A &amp; 1))

int mult(unsigned int a, unsigned int b)
{
    int prod = 0;
    do {
        if EVEN(a) 
            prod += b;
        a &gt;&gt;= 1;
        b &lt;&lt;= 1;
    } while (a &gt;= 1); 
    printf(&quot;Le produit est: %d\n&quot;, prod);
    return prod;
}[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>I&#8217;ll break this down in three replies, C is just too verbose. I&#8217;m fascinated by how compact your Scheme and Haskell solutions always are&#8230; I really wanted to have a serious look at Python as a next step at learning to program, but this could make me change my mind.</p>
<p>Anyway, multiplication:</p>
<pre class="brush: cpp;">
#define EVEN(A) (!(A &amp; 1))

int mult(unsigned int a, unsigned int b)
{
    int prod = 0;
    do {
        if EVEN(a)
            prod += b;
        a &gt;&gt;= 1;
        b &lt;&lt;= 1;
    } while (a &gt;= 1);
    printf(&quot;Le produit est: %d\n&quot;, prod);
    return prod;
}</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remco Niemeijer</title>
		<link>http://programmingpraxis.com/2010/01/15/three-binary-algorithms/#comment-918</link>
		<dc:creator><![CDATA[Remco Niemeijer]]></dc:creator>
		<pubDate>Fri, 15 Jan 2010 13:25:22 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1887#comment-918</guid>
		<description><![CDATA[My Haskell solution (see http://bonsaicode.wordpress.com/2010/01/15/programming-praxis-three-binary-algorithms/ for a version with comments):

[sourcecode lang=&quot;css&quot;]
import Data.Bits

left, right :: Bits a =&gt; a -&gt; a
left = flip shiftL 1
right = flip shiftR 1

binmult :: (Bits a, Integral a) =&gt; a -&gt; a -&gt; a
binmult 1 b = b
binmult a b = binmult (right a) (left b) + if odd a then b else 0

bindiv :: (Bits a, Ord a) =&gt; a -&gt; a -&gt; (a, a)
bindiv n d = f (right $ until (&gt; n) left d) 0 n where
    f t q r &#124; t &lt; d     = (q, r)
            &#124; t &lt;= r    = f (right t) (left q + 1) (r - t)
            &#124; otherwise = f (right t) (left q)     r

bingcd :: (Bits a, Integral a) =&gt; a -&gt; a -&gt; a
bingcd a 0 = a
bingcd 0 b = b
bingcd a b &#124; even a &amp;&amp; even b = 2 * bingcd (right a) (right b)
           &#124; even a           = bingcd (right a) b
           &#124; even b           = bingcd a (right b)
           &#124; a &gt; b            = bingcd (a - b) b
           &#124; otherwise        = bingcd a (b - a)
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>My Haskell solution (see <a href="http://bonsaicode.wordpress.com/2010/01/15/programming-praxis-three-binary-algorithms/" rel="nofollow">http://bonsaicode.wordpress.com/2010/01/15/programming-praxis-three-binary-algorithms/</a> for a version with comments):</p>
<pre class="brush: css;">
import Data.Bits

left, right :: Bits a =&gt; a -&gt; a
left = flip shiftL 1
right = flip shiftR 1

binmult :: (Bits a, Integral a) =&gt; a -&gt; a -&gt; a
binmult 1 b = b
binmult a b = binmult (right a) (left b) + if odd a then b else 0

bindiv :: (Bits a, Ord a) =&gt; a -&gt; a -&gt; (a, a)
bindiv n d = f (right $ until (&gt; n) left d) 0 n where
    f t q r | t &lt; d     = (q, r)
            | t &lt;= r    = f (right t) (left q + 1) (r - t)
            | otherwise = f (right t) (left q)     r

bingcd :: (Bits a, Integral a) =&gt; a -&gt; a -&gt; a
bingcd a 0 = a
bingcd 0 b = b
bingcd a b | even a &amp;&amp; even b = 2 * bingcd (right a) (right b)
           | even a           = bingcd (right a) b
           | even b           = bingcd a (right b)
           | a &gt; b            = bingcd (a - b) b
           | otherwise        = bingcd a (b - a)
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Programming Praxis &#8211; Three Binary Algorithms &#171; Bonsai Code</title>
		<link>http://programmingpraxis.com/2010/01/15/three-binary-algorithms/#comment-917</link>
		<dc:creator><![CDATA[Programming Praxis &#8211; Three Binary Algorithms &#171; Bonsai Code]]></dc:creator>
		<pubDate>Fri, 15 Jan 2010 13:25:09 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1887#comment-917</guid>
		<description><![CDATA[[...] Praxis &#8211; Three Binary&#160;Algorithms By Remco Niemeijer  In today&#8217;s Programming Praxis we have to implement binary algorithms for multiplying, dividing, and finding [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Praxis &#8211; Three Binary&nbsp;Algorithms By Remco Niemeijer  In today&#8217;s Programming Praxis we have to implement binary algorithms for multiplying, dividing, and finding [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

