{"id":54,"date":"2008-03-01T15:56:24","date_gmt":"2008-03-01T17:56:24","guid":{"rendered":"http:\/\/www.ragestorm.net\/blogs\/?p=54"},"modified":"2008-03-01T15:56:24","modified_gmt":"2008-03-01T17:56:24","slug":"basic-ops-4-division","status":"publish","type":"post","link":"https:\/\/www.ragestorm.net\/blogs\/?p=54","title":{"rendered":"Basic Ops #4 &#8211; Division"},"content":{"rendered":"<p>I wasn&#8217;t sure whether to post this one or not. More over, my dilemma was whether to document this one or not. I decided that it&#8217;s so hard to explain what&#8217;s going on, that I will only dump my code here. The interface is ugly, because you cannot divide by zero&#8230;<\/p>\n<p>\/\/ return success, *ret = a \/ b<br \/>\nint div(unsigned short a, unsigned short b, unsigned short *ret)<br \/>\n{<br \/>\n\u00a0if (b == 0) return 0;<br \/>\n\u00a0*ret = 0;<br \/>\n\u00a0unsigned short mask = 0, len = 0;<\/p>\n<p>\u00a0\/\/ calc the mask of b.<br \/>\n\u00a0for (int i = 15; i &gt;= 0; i&#8211;) {<br \/>\n\u00a0 if (b &amp; (1 &lt;&lt; i)) {<br \/>\n\u00a0  mask = (1 &lt;&lt; i) &#8211; 1;<br \/>\n\u00a0  len = 16 &#8211; i;<br \/>\n\u00a0  break;<br \/>\n\u00a0 }<br \/>\n\u00a0}<br \/>\n\u00a0\/\/ complement mask.<br \/>\n\u00a0mask = ~mask;<\/p>\n<p>\u00a0\/\/ left justify b.<br \/>\n\u00a0while ((~b) &amp; (1 &lt;&lt; 15)) b &lt;&lt;= 1;<\/p>\n<p>\u00a0while (len&#8211;) {<br \/>\n\u00a0 *ret &lt;&lt;= 1;<br \/>\n\u00a0 if ((a &amp; mask) &gt;= b) {<br \/>\n\u00a0\u00a0 *ret |= 1;<br \/>\n\u00a0\u00a0 a -= b;<br \/>\n\u00a0 }<br \/>\n\u00a0 b &gt;&gt;= 1;<br \/>\n\u00a0 mask &gt;&gt;= 1;<br \/>\n\u00a0}<\/p>\n<p>\u00a0return 1;<br \/>\n}<\/p>\n<p>And yes it&#8217;s 16 bits division, it will apply for 32 or 64 bits as well. It really was one of the toughest things to write from scratch. Division is not that trivial like subtraction or multiplication. The trick eventually was to use the fact that we&#8217;re dealing with binary division..oh well, I quit. Get someone else to describe this one. Although I think there are many ways to implement this one. I remember a friend telling me he didn&#8217;t expect this way of solution. I don&#8217;t think, however, that this technique may be used for long division for bit array or really big numbers, maybe there&#8217;s something faster.<\/p>\n<p>By the way, this is an <em>unsigned<\/em> division. As far as I know, and I might be wrong, you can&#8217;t do signed division without using unsigned division. And to do signed division you just remember the sign of a and b and then make them absolute and then you change the result accordingly. Anyways, if I&#8217;m wrong with my assumption, let me know otherwise.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I wasn&#8217;t sure whether to post this one or not. More over, my dilemma was whether to document this one or not. I decided that it&#8217;s so hard to explain what&#8217;s going on, that I will only dump my code here. The interface is ugly, because you cannot divide by zero&#8230; \/\/ return success, *ret [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":""},"categories":[21],"tags":[],"jetpack_featured_media_url":"","jetpack_publicize_connections":[],"jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/pbWKd-S","_links":{"self":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/54"}],"collection":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=54"}],"version-history":[{"count":0,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=\/wp\/v2\/posts\/54\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=54"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=54"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.ragestorm.net\/blogs\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=54"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}