{"id":7043,"date":"2025-07-05T00:01:11","date_gmt":"2025-07-05T07:01:11","guid":{"rendered":"https:\/\/c-for-dummies.com\/blog\/?p=7043"},"modified":"2025-07-17T10:40:13","modified_gmt":"2025-07-17T17:40:13","slug":"base-36-powers","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=7043","title":{"rendered":"Base 36 Powers"},"content":{"rendered":"<p>To build a number in base 36, you need to know the powers of base 36. This information is required to output digits &mdash; 0 through 9 and then A through Z &mdash; in the proper order to represent a base 36 value. This task may seem complicated, but it&#8217;s the same process that takes place for any counting base.<br \/>\n<!--more--><br \/>\nFor example, in base 10, the powers are:<\/p>\n<p>1<br \/>\n10<br \/>\n100<br \/>\n1,000<\/p>\n<p>And so on. For example, and illustrated in Figure 1, the value 4,096 represents 4 thousands, zero hundreds, 9 tens, and 6 ones. Most humans don&#8217;t deconstruct numbers in this manner because base 10 (decimal) is what we use.<\/p>\n<div id=\"attachment_7055\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7055\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/07\/0705-figure1.png\" alt=\"\" width=\"500\" height=\"445\" class=\"size-full wp-image-7055\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/07\/0705-figure1.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/07\/0705-figure1-300x267.png 300w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/07\/0705-figure1-337x300.png 337w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><p id=\"caption-attachment-7055\" class=\"wp-caption-text\">Figure 1. How the decimal value 4,096 works based on powers of 10.<\/p><\/div>\n<p>To continue from <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7037\">last week&#8217;s Lesson<\/a>, and represent a number in base 36, I&#8217;ve wrote the following code as a start. It builds and outputs the base 36 powers table:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_07_05-Lesson-a.c\" rel=\"noopener\" target=\"_blank\">2025_07_05-Lesson-a.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define BASE36_MAX 36\r\n#define SIZE 10\r\n\r\nint main()\r\n{\r\n    long b36_powers[SIZE];\r\n    int x;\r\n\r\n    <span class=\"comments\">\/* build the powers table *\/<\/span>\r\n    for( x=0; x&lt;SIZE; x++ )\r\n        b36_powers[x] = x ? b36_powers[x-1]*BASE36_MAX : 1;\r\n\r\n    <span class=\"comments\">\/* output the powers table *\/<\/span>\r\n    for( x=0; x&lt;SIZE; x++ )\r\n        printf(\"36^%d = %ld\\n\",x,b36_powers[x]);\r\n\r\n    <span class=\"comments\">\/* clean-up *\/<\/span>\r\n    return 0;\r\n}<\/pre>\n<p>The <code>b36_powers[]<\/code> array holds values representing powers 36<sup>0<\/sup> through 36<sup>9<\/sup>. The following expression is what builds the array:<\/p>\n<p><code>x ? b36_powers[x-1]*BASE36_MAX : 1<\/code><\/p>\n<p>As the value of <code>x<\/code> increases, the expression <code>b36_powers[x-1]*BASE36_MAX<\/code> provides the proper power of 36. It takes the previous array value (1 to start) and multiplies it by the <code>BASE35_MAX<\/code> constant, 36. The loop fills the table, which is then output:<\/p>\n<p><code>36^0 = 1<br \/>\n36^1 = 36<br \/>\n36^2 = 1296<br \/>\n36^3 = 46656<br \/>\n36^4 = 1679616<br \/>\n36^5 = 60466176<br \/>\n36^6 = 2176782336<br \/>\n36^7 = 78364164096<br \/>\n36^8 = 2821109907456<br \/>\n36^9 = 101559956668416<\/code><\/p>\n<p>These are the powers of 36, which are used to translate a decimal value into base 36.<\/p>\n<p>Once the values are known, the following expression sets the proper digits into the correct positions:<\/p>\n<p><code>( d % b36_powers[x+1])\/ b36_powers[x]<\/code><\/p>\n<p>Variable <code>d<\/code> is a decimal value. Its modulus is calculated for a specific value from the <code>b36_powers[]<\/code> array. The result is divided by the previous powers value, which yields how many units of a certain power translate from decimal to base 36.<\/p>\n<p>Here is the code where this expression works to translate a decimal value into base 36. The output shows how the base 36 powers equivalent of the decimal value:<\/p>\n<h3><a href=\"https:\/\/github.com\/dangookin\/C-For-Dummies-Blog\/blob\/master\/2025_07_05-Lesson-b.c\" rel=\"noopener\" target=\"_blank\">2025_07_05-Lesson-b.c<\/a><\/h3>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\n#define BASE36_MAX 36\r\n#define SIZE 10\r\n\r\nint main()\r\n{\r\n    long b36_powers[SIZE];\r\n    int b36_values[SIZE];\r\n    int x,d;\r\n\r\n    <span class=\"comments\">\/* prompt for input *\/<\/span>\r\n    printf(\"Enter base 10 value: \");\r\n    scanf(\"%d\",&amp;d);\r\n    if( d&lt;1 )\r\n    {\r\n        puts(\"Enter a value larger than zero\");\r\n        return 1;\r\n    }\r\n\r\n    <span class=\"comments\">\/* build the powers table *\/<\/span>\r\n    for( x=0; x&lt;SIZE; x++ )\r\n        b36_powers[x] = x ? b36_powers[x-1]*BASE36_MAX : 1;\r\n\r\n    <span class=\"comments\">\/* translate the decimal value *\/<\/span>\r\n    for( x=SIZE-2; x&gt;=0; x-- )\r\n        b36_values[x+1] = ( d % b36_powers[x+1])\/ b36_powers[x];\r\n\r\n    <span class=\"comments\">\/* output the values array *\/<\/span>\r\n    for( x=1; x&lt;SIZE; x++ )\r\n        printf(\"36^%d = %d\\n\",x,b36_values[x]);\r\n\r\n    <span class=\"comments\">\/* clean-up *\/<\/span>\r\n    return 0;\r\n}<\/pre>\n<p>This code prompts the user for a decimal value. The base 36 powers table is created. The clever expression is used on the decimal value input to pull out the powers of base 36 relative to the decimal value input. The loop runs in reverse order, peeling off larger values first:<\/p>\n<p><code>for( x=SIZE-2; x&gt;=0; x-- )<\/code><\/p>\n<p>The loop stops before <code>x<\/code> is equal to zero because 36<sup>0<\/sup> is one, which won&#8217;t help with the translation. (It just adds an extra zero to the result.)<\/p>\n<p>The output shows how the decimal value is translated into base 36 powers:<\/p>\n<p><code>Enter base 10 value: 12345<br \/>\n36^1 = 33<br \/>\n36^2 = 18<br \/>\n36^3 = 9<br \/>\n36^4 = 0<br \/>\n36^5 = 0<br \/>\n36^6 = 0<br \/>\n36^7 = 0<br \/>\n36^8 = 0<br \/>\n36^9 = 0<\/code><\/p>\n<p>For the decimal value 12,345 the result is (skipping leading zeros): 9, 18, 33 in base 36. These values are base 36 digits, placeholders for certain powers. Using my 0-9 A-Z representation, the value of 12,3456 in base 36 is 9IX. This value is described in Figure 2.<\/p>\n<div id=\"attachment_7054\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-7054\" src=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/07\/0705-figure2.png\" alt=\"\" width=\"500\" height=\"286\" class=\"size-full wp-image-7054\" srcset=\"https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/07\/0705-figure2.png 500w, https:\/\/c-for-dummies.com\/blog\/wp-content\/uploads\/2025\/07\/0705-figure2-300x172.png 300w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><p id=\"caption-attachment-7054\" class=\"wp-caption-text\">Figure 2. How the base 36 value 9IX translates into decimal.<\/p><\/div>\n<p>In <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7058\">next week&#8217;s Lesson<\/a>, I add a function that translates these values into a base 36 string for output.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To output a value in any base, you must first build a powers table. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=7043\">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-7043","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\/7043","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=7043"}],"version-history":[{"count":6,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7043\/revisions"}],"predecessor-version":[{"id":7081,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/7043\/revisions\/7081"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=7043"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=7043"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=7043"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}