{"id":3587,"date":"2019-05-08T00:01:04","date_gmt":"2019-05-08T07:01:04","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3587"},"modified":"2019-05-06T09:40:28","modified_gmt":"2019-05-06T16:40:28","slug":"primitive-math-solution","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3587","title":{"rendered":"Primitive Math &#8211; Solution"},"content":{"rendered":"<p>The solution to <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3578\">this month&#8217;s Exercise<\/a> involves two things. First, knowing how to shift bits and second how to carefully enclose operators in parentheses to get the macros to behave.<br \/>\n<!--more--><br \/>\nThe left-shift operator, &lt;&lt;, represents the fastest way to multiply a value. Here&#8217;s a crib sheet:<\/p>\n<p><code>a&lt;&lt;1<\/code> double the value of <code>a<\/code> or <code>a<sup>2<\/sup><\/code><br \/>\n<code>a&lt;&lt;2<\/code> quadruple the value of <code>a<\/code> or <code>a<sup>3<\/sup><\/code><br \/>\n<code>a&lt;&lt;3<\/code> double-quadruple the value of <code>a<\/code> or <code>a<sup>4<\/sup><\/code><\/p>\n<p>Yes, it&#8217;s our old programming buddies the powers of two: Keep shifting bits and you march through all of them: <code>*2<\/code>, <code>*4<\/code>, <code>*8<\/code>, on up to however wide the bitfield. You can read more about the power of the bit-shift in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=592\">this blog post<\/a>.<\/p>\n<p>The macros you create as a solution for <em>BYTWO()<\/em>, <em>BYFOUR()<\/em>, and <em>BYEIGHT()<\/em> are the easiest to write &mdash; assuming that you&#8217;re taking advantage of the shift operator:<\/p>\n<p><code>#define BYTWO(a) a&lt;&lt;1<br \/>\n#define BYFOUR(a) a&lt;&lt;2<br \/>\n#define BYEIGHT(a) a&lt;&lt;3<\/code><\/p>\n<p>And, of course, the <em>BYONE()<\/em> macro isn&#8217;t that difficult to write, but it was part of the challenge:<\/p>\n<p><code>#define BYONE(a) a<\/code><\/p>\n<p>For the rest, combine these techniques, but only with a generous application of parentheses to keep the operations tight. To wit:<\/p>\n<p><code>#define BYTHREE(a) a+(a<<1)<\/code><\/p>\n<p>Three is *1 + *2, but you must write the *2 in parentheses: <code>(a<<1)<\/code> If you don't, the compiler gets all huffy.<\/p>\n<p>You can see the rest of my macros in my solution here:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define BYONE(a) a\r\n#define BYTWO(a) a&lt;&lt;1\r\n#define BYTHREE(a) a+(a&lt;&lt;1)\r\n#define BYFOUR(a) a&lt;&lt;2\r\n#define BYFIVE(a) a+(a&lt;&lt;2)\r\n#define BYSIX(a) (a&lt;&lt;1)+(a&lt;&lt;1)+(a&lt;&lt;1)\r\n#define BYSEVEN(a) a+(a&lt;&lt;2)+(a&lt;&lt;1)\r\n#define BYEIGHT(a) a&lt;&lt;3\r\n#define BYNINE(a) a+(a&lt;&lt;3)\r\n#define BYTEN(a) (a&lt;&lt;3)+(a&lt;&lt;1)\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's sample output:<\/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>I poured over some of my old Assembly code from decades past and couldn't find a specific routine where I included similar instructions for performing basic math. Most of what I did back when was pretty direct. For example, when I needed to triple a value, I shifted it left and then added it to itself immediately as opposed to writing a specific subroutine.<\/p>\n<p>Anyway, I hope you came up with some clever solutions. Obviously, such macros aren't vital to solving any specific problem in the C language; plenty of math operators and functions perform arithmetic better. Just keep in mind the macro solution for some simple problems, especially if the macro names help make your code more readable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The solution to this month&#8217;s Exercise involves two things. First, knowing how to shift bits and second how to carefully enclose operators in parentheses to get the macros to behave.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5],"tags":[],"class_list":["post-3587","post","type-post","status-publish","format-standard","hentry","category-solution"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3587","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=3587"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3587\/revisions"}],"predecessor-version":[{"id":3598,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3587\/revisions\/3598"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3587"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3587"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3587"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}