{"id":3563,"date":"2019-04-13T00:01:54","date_gmt":"2019-04-13T07:01:54","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=3563"},"modified":"2019-04-06T11:27:45","modified_gmt":"2019-04-06T18:27:45","slug":"macros-galore","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=3563","title":{"rendered":"Macros Galore!"},"content":{"rendered":"<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3553\">last week&#8217;s Lesson<\/a>, I demonstrated how macros can save time, effectively replacing one-line functions in your code. One place I&#8217;ve used macros in my own code is when performing binary math operations.<br \/>\n<!--more--><br \/>\nBinary math isn&#8217;t anything you need to do in the C language. C is capable because it&#8217;s a mid-level language, but it also includes mathematical operators and functions that operate at a high level. Back in my assembly language days, however, binary math was something you had to program.<\/p>\n<p>The processors back in the old days lacked complex mathematic instructions. For example, if you wanted to multiply a value by six you&#8217;d perform these steps:<\/p>\n<ol>\n<li>Shift the value left one notch, doubling it. Save that value.<\/li>\n<li>Add the new value to itself twice.<\/li>\n<\/ol>\n<p>In C, you express this operation on the value in variable <code>a<\/code> as follows:<\/p>\n<p><code>a &lt;&lt;= 1;<br \/>\na += a + a;<\/code><\/p>\n<p>The first statement doubles the value in a, so 5 becomes 10.<\/p>\n<p>The second statement adds 10 to itself once for 20 and to itself again for 30: 5 * 6 = 30.<\/p>\n<p>These statements represent molecules in binary arithmetic, performing simple functions that carry out basic math. They&#8217;re ideal for inclusion in your code as macros. To wit:<\/p>\n<p><code>#define BYTWO(a) a&lt;&lt1<\/code><\/p>\n<p>The <em>BYTWO()<\/em> macro multiplies the value of variable <code>a<\/code> by two, doubling it. The macro needs an lvalue, so the result must be assigned as in:<\/p>\n<p><code>x = BYTWO(4);<\/code><\/p>\n<p>This macro triples a value:<\/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 its double, which must also be assigned as in:<\/p>\n<p><code>x = BYTHREE(4);<\/code><\/p>\n<p>Of course, all these macros are silly given that C features math operators that let you multiply any value by any other. At the microscopic level, bit shifting for multiplication is faster than higher-order operations, though at today&#8217;s processor speeds the performance boost you see is minimal. Still, I think it&#8217;s fun to play with such macros and concoct methods of performing math at the binary level.<\/p>\n<p>Here&#8217;s another macro I&#8217;ve used in my code:<\/p>\n<p><code>#define NEWLINE() putchar('\\n')<\/code><\/p>\n<p>The <em>NEWLINE()<\/em> macro outputs the newline character, which helps clean up program output. It&#8217;s handy.<\/p>\n<p>Creating macros can be fun, but avoid their over-use just to be cute. It&#8217;s possible (and I&#8217;ve seen it done) to obfuscate C code to make it look like a completely different language. While such efforts can be the foundation of curiosity or discussion, they detract from the bigger picture goal of keeping your code readable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Macros can be used to effectively streamline your code and make it more readable. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=3563\">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":[2],"tags":[],"class_list":["post-3563","post","type-post","status-publish","format-standard","hentry","category-main"],"_links":{"self":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3563","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=3563"}],"version-history":[{"count":3,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3563\/revisions"}],"predecessor-version":[{"id":3567,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/3563\/revisions\/3567"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}