{"id":4149,"date":"2020-05-30T00:01:53","date_gmt":"2020-05-30T07:01:53","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=4149"},"modified":"2020-06-06T08:47:28","modified_gmt":"2020-06-06T15:47:28","slug":"powers-of-three","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=4149","title":{"rendered":"Powers of Three"},"content":{"rendered":"<p>When the math nerds refer to a counting system, they use the word <em>base<\/em>. &#8220;We count in base 10,&#8221; they proclaim, adding, &#8220;Decimal&#8221; to sound important. Surely, these are the miracles of mathematics.<br \/>\n<!--more--><br \/>\nCounting systems come into play for programming all the time. To work with digital information, you must grasp binary or &#8220;base two.&#8221; This system consists of one and zeros and it&#8217;s easy to cross your eyes when looking at a binary value:<\/p>\n<p><code>1011011100001001<\/code><\/p>\n<p>So the nerds break up the binary number into clumps of four digits:<\/p>\n<p><code>1011 0111 0000 1001<\/code><\/p>\n<p>Then to save time and space, they translate the value into hexadecimal, the base 16 counting system that has nothing to do with Harry Potter:<\/p>\n<p><code>0xB709<\/code><\/p>\n<p>I&#8217;m such a nerd, I was able to translate the binary into hex without looking it up. You can achieve such nerdhood as well! Regardless, ancient neckbeards also know base 8, octal. Here is how you see binary through octal spectacles:<\/p>\n<p><code>1 011 011 100 001 001<\/code><\/p>\n<p>This value translates into:<\/p>\n<p><code>0133411<\/code><\/p>\n<p>The leading zero identifies the value as octal.<\/p>\n<p>For all counting bases, the methodology works the same: Working left to right, each digit place represents the next highest power of the base value. Figure 1 illustrates how this operation works for any base numbering system.<\/p>\n<div id=\"attachment_4154\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-4154\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/05\/06exercise-Figure1.png\" alt=\"\" width=\"550\" height=\"78\" class=\"size-full wp-image-4154\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/05\/06exercise-Figure1.png 550w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/05\/06exercise-Figure1-300x43.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2020\/05\/06exercise-Figure1-500x71.png 500w\" sizes=\"auto, (max-width: 550px) 100vw, 550px\" \/><p id=\"caption-attachment-4154\" class=\"wp-caption-text\">Figure 1. How counting systems represent their values.<\/p><\/div>\n<p>For base 10, the digits marching from right to left represent ones, tens, hundreds, thousands, and so on. For base 16 the digits are ones, sixteens, 256s, and so on. You get the picture: If you want to represent any counting base system, each column represents an increased power of the one to its right.<\/p>\n<p>So what if I want to write code that deals with base 3, the ternary (not trinary) counting system? Why, if I really wanted to achieve such a useless level of geekdom, I&#8217;d start with the powers of three!<\/p>\n<p>The sample code shown below generates a power table based on the constant <code>base<\/code>. I avoided using the <em>pow()<\/em> function and instated relied upon storing each value in the <code>powers[]<\/code> array, which comes in handy later when I use a powers table to concoct a counting system.<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2020_05_30-Lesson.c\" rel=\"noopener noreferrer\" target=\"_blank\">2020_05_30-Lesson.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    const int base = 3;\r\n    int powers[11];\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* create the powers table *\/<\/span>\r\n    powers[0] = 1;\r\n    for(x=1;x&lt;11;x++)\r\n    {\r\n        powers[x] = powers[x-1]*base;\r\n    }\r\n\r\n    for(x=0;x&lt;11;x++)\r\n        printf(\"%d^%d = %d\\n\",base,x,powers[x]);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Each element number in the array represents a power. Element zero is set to one at Line 10. This preset explains why the loop goes from 1 to 11 instead of starting at zero. It also benefits Line 13, which uses the preceding element (<code>powers[x-1]<\/code>) to calculate the current element as a power.<\/p>\n<p>Once the array is built, it&#8217;s output by a <em>for<\/em> loop at Line 16.<\/p>\n<p>Here is a sample run:<\/p>\n<p><code>3^0 = 1<br \/>\n3^1 = 3<br \/>\n3^2 = 9<br \/>\n3^3 = 27<br \/>\n3^4 = 81<br \/>\n3^5 = 243<br \/>\n3^6 = 729<br \/>\n3^7 = 2187<br \/>\n3^8 = 6561<br \/>\n3^9 = 19683<br \/>\n3^10 = 59049<\/code><\/p>\n<p>As a constant, you can modify variable <code>base<\/code> to any other value to generate another powers table. Here&#8217;s the output when I change <code>base<\/code> to 5:<\/p>\n<p><code>5^0 = 1<br \/>\n5^1 = 5<br \/>\n5^2 = 25<br \/>\n5^3 = 125<br \/>\n5^4 = 625<br \/>\n5^5 = 3125<br \/>\n5^6 = 15625<br \/>\n5^7 = 78125<br \/>\n5^8 = 390625<br \/>\n5^9 = 1953125<br \/>\n5^10 = 9765625<\/code><\/p>\n<p>If you want to use larger values for <code>base<\/code>, change Line 7 so that variable <code>x<\/code> is a <em>long<\/em>.<\/p>\n<p>My nefarious purpose here is create a powers of three table. Such a table comes in handy when writing functions that input and output values in ternary. This process starts in <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4172\">next week&#8217;s Lesson<\/a>.<\/p>\n<p>Yes, I&#8217;m that crazy.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Your programming career counts from decimal to hexadecimal with perhaps some binary and octal thrown in. What about ternary? <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=4149\">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-4149","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\/4149","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=4149"}],"version-history":[{"count":8,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4149\/revisions"}],"predecessor-version":[{"id":4201,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4149\/revisions\/4201"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}