{"id":3578,"date":"2019-05-01T00:01:21","date_gmt":"2019-05-01T07:01:21","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3578"},"modified":"2023-02-10T20:18:56","modified_gmt":"2023-02-11T04:18:56","slug":"primitive-math","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3578","title":{"rendered":"Primitive Math"},"content":{"rendered":"<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3563\">a recent Lesson<\/a>, I explored upon the idea of creating binary math macros. This technique was popular in Assembly language programs where the processor lacks high order math functions, including basic multiplication and division. In C you can code one-line macros that mimic Assembly&#8217;s bit-shifting behavior required to multiply values.<br \/>\n<!--more--><br \/>\nFor example, to multiple a value by three, the following macro is used:<\/p>\n<p><code>#define BYTHREE(a) a+(a&lt;&lt;1)<\/code><\/p>\n<p>The value of variable <code>a<\/code> is added to itself shifted left one spot, which is essentially <code>a+(a*2)<\/code>, or <code>3*a<\/code>.<\/p>\n<p>For this month&#8217;s Exercise, you are to write 11 such macros, each corresponding to a multiplication value:<\/p>\n<p><em>BYONE(a)<br \/>\nBYTWO(a)<br \/>\nBYTHREE(a)<br \/>\nBYFOUR(a)<br \/>\nBYFIVE(a)<br \/>\nBYSIX(a)<br \/>\nBYSEVEN(a)<br \/>\nBYEIGHT(a)<br \/>\nBYNINE(a)<br \/>\nBYTEN(a)<\/em><\/p>\n<p>These macros are used to manipulate the value 6, as shown by this code skeleton:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n<span class=\"comments\">\/* Macros go here *\/<\/span>\r\n\r\nint main()\r\n{\r\n    printf(\"%d *  1 = %d\\n\",6,BYONE(6));\r\n    printf(\"%d *  2 = %d\\n\",6,BYTWO(6));\r\n    printf(\"%d *  3 = %d\\n\",6,BYTHREE(6));\r\n    printf(\"%d *  4 = %d\\n\",6,BYFOUR(6));\r\n    printf(\"%d *  5 = %d\\n\",6,BYFIVE(6));\r\n    printf(\"%d *  6 = %d\\n\",6,BYSIX(6));\r\n    printf(\"%d *  7 = %d\\n\",6,BYSEVEN(6));\r\n    printf(\"%d *  8 = %d\\n\",6,BYEIGHT(6));\r\n    printf(\"%d *  9 = %d\\n\",6,BYNINE(6));\r\n    printf(\"%d * 10 = %d\\n\",6,BYTEN(6));\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s sample output, assuming the macros are created and added to the above skeleton:<\/p>\n<p><code>6 *  1 = 6<br \/>\n6 *  2 = 12<br \/>\n6 *  3 = 18<br \/>\n6 *  4 = 24<br \/>\n6 *  5 = 30<br \/>\n6 *  6 = 36<br \/>\n6 *  7 = 42<br \/>\n6 *  8 = 48<br \/>\n6 *  9 = 54<br \/>\n6 * 10 = 60<\/code><\/p>\n<p>The macros can be coded in a number of ways, though you are restricted to using the + and &lt;&lt; shift operators. And while you can just use addition, as in <code>a+a+a<\/code> for <em>BYTHREE()<\/em>, don&#8217;t. See how clever you can be with the left-shift operator to keep the macros as tight as possible. Further, don&#8217;t try to use the definition of one macro within another.<\/p>\n<p>Please try this Exercise on your own before you check <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3587\">my solution<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In a recent Lesson, I explored upon the idea of creating binary math macros. This technique was popular in Assembly language programs where the processor lacks high order math functions, including basic multiplication and division. In C you can code &hellip; <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3578\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-3578","post","type-post","status-publish","format-standard","hentry","category-exercise"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3578","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3578"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3578\/revisions"}],"predecessor-version":[{"id":5759,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3578\/revisions\/5759"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3578"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3578"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3578"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}