{"id":2388,"date":"2017-03-04T00:01:59","date_gmt":"2017-03-04T08:01:59","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=2388"},"modified":"2017-02-25T09:57:26","modified_gmt":"2017-02-25T17:57:26","slug":"the-ghost-of-octal","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=2388","title":{"rendered":"The Ghost of Octal"},"content":{"rendered":"<p>Programmers become exposed to multiple counting bases as well as ways of representing values. As a human, you work in base 10, probably thanks to your 10 fingers. When you program, you learn about base 2 binary and base 16 hexadecimal. You study exponential notation as well, especially for crunching very large or very small values.<\/p>\n<p>Somewhere in the mire, you encounter base 8, octal. You nod appropriately at the information as it&#8217;s glossed over, then you move on and never return to discover what was once a huge deal.<br \/>\n<!--more--><br \/>\nProgrammers of the 1960s and 1970s ate octal for breakfast. They knew octal codes the way today&#8217;s nerds have memorized hexadecimal values and their binary bit patterns. That&#8217;s because computers of the era, called <em>minicomputers<\/em>, didn&#8217;t use 8-bit bytes. They used 36-bit bytes or <em>words<\/em>.<\/p>\n<p>A 36-bit word can be broken up into 12 chunks of 3-bits each. And in binary, 3 bits represent values from 0 to 7 &mdash; base 8 octal:<\/p>\n<table border=\"0\">\n<tr>\n<th>Bits<\/th>\n<th>Octal<\/th>\n<th>Decimal<\/th>\n<\/tr>\n<tr>\n<td>000<\/td>\n<td>00<\/td>\n<td>0<\/td>\n<\/tr>\n<tr>\n<td>001<\/td>\n<td>01<\/td>\n<td>1<\/td>\n<\/tr>\n<tr>\n<td>010<\/td>\n<td>02<\/td>\n<td>2<\/td>\n<\/tr>\n<tr>\n<td>011<\/td>\n<td>03<\/td>\n<td>3<\/td>\n<\/tr>\n<tr>\n<td>100<\/td>\n<td>04<\/td>\n<td>4<\/td>\n<\/tr>\n<tr>\n<td>101<\/td>\n<td>05<\/td>\n<td>5<\/td>\n<\/tr>\n<tr>\n<td>110<\/td>\n<td>06<\/td>\n<td>6<\/td>\n<\/tr>\n<tr>\n<td>111<\/td>\n<td>07<\/td>\n<td>7<\/td>\n<\/tr>\n<\/table>\n<p>What comes after 7? Well, 8 of course! But in base 8, the value 8 is written as <code>10<\/code>. (In base 2, the value 2 is written as <code>10<\/code>; in base 10 the value 10 is written as <code>10<\/code>; in base 16, the value 16 is written as <code>10<\/code>.)<\/p>\n<p>To avoid confusing octal with decimal values, in the C language octal values are prefixed with a zero: <code>010<\/code> is octal 10. If you use a color-coded editor, the initial zero in an octal value is given a different color to help express and identify the value as octal, not decimal. (If the initial zero is followed by a period, then the value is a decimal real number, not octal.)<\/p>\n<p>Rules regarding octal values were established in the B programming language and inherited by C when it was developed in the 1970s. Because those programmers worked in 36-bit minicomputer environments, octal was handy and used frequently. In fact, because C is the ancestor of nearly every popular programming language today, octal remains as a valid number format. Some languages even borrow the leading zero for octal notation.<\/p>\n<p>This ancestry also explains why single-character escape sequence uses octal instead of decimal or hex. For example:<\/p>\n<p><code>a = '\\100';<\/code><\/p>\n<p>The above statement assigns <em>octal<\/em> value 100 (or <span style=\"color:red\">0<\/span><span style=\"color:blue\">100<\/span>) to variable <code>a<\/code>. That&#8217;s the <code>'@'<\/code> character, decimal value 64.<\/p>\n<p>The following code generates a table of the first 64 octal values, along with their decimal and hexadecimal equivalents:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int a;\r\n\r\n    puts(\"Oct Dec Hex\\tOct Dec Hex\\tOct Dec Hex\\tOct Dec Hex\");\r\n    for(a=00;a&lt;=017;a++)\r\n    {\r\n        printf(\"%3o %3d %3X\\t\",a,a,a);\r\n        printf(\"%3o %3d %3X\\t\",a+020,a+020,a+020);\r\n        printf(\"%3o %3d %3X\\t\",a+040,a+040,a+040);\r\n        printf(\"%3o %3d %3X\\n\",a+060,a+060,a+060);\r\n    }\r\n\r\n    return(0);\r\n}<\/pre>\n<p>All values in the above code are expressed as octal; they feature a leading zero. The <code>%o<\/code> placeholder in the <em>printf()<\/em> statements generates octal output.<\/p>\n<p>Here&#8217;s a sample run:<\/p>\n<pre><code>Oct Dec Hex\tOct Dec Hex\tOct Dec Hex\tOct Dec Hex\r\n  0   0   0\t 20  16  10\t 40  32  20\t 60  48  30\r\n  1   1   1\t 21  17  11\t 41  33  21\t 61  49  31\r\n  2   2   2\t 22  18  12\t 42  34  22\t 62  50  32\r\n  3   3   3\t 23  19  13\t 43  35  23\t 63  51  33\r\n  4   4   4\t 24  20  14\t 44  36  24\t 64  52  34\r\n  5   5   5\t 25  21  15\t 45  37  25\t 65  53  35\r\n  6   6   6\t 26  22  16\t 46  38  26\t 66  54  36\r\n  7   7   7\t 27  23  17\t 47  39  27\t 67  55  37\r\n 10   8   8\t 30  24  18\t 50  40  28\t 70  56  38\r\n 11   9   9\t 31  25  19\t 51  41  29\t 71  57  39\r\n 12  10   A\t 32  26  1A\t 52  42  2A\t 72  58  3A\r\n 13  11   B\t 33  27  1B\t 53  43  2B\t 73  59  3B\r\n 14  12   C\t 34  28  1C\t 54  44  2C\t 74  60  3C\r\n 15  13   D\t 35  29  1D\t 55  45  2D\t 75  61  3D\r\n 16  14   E\t 36  30  1E\t 56  46  2E\t 76  62  3E\r\n 17  15   F\t 37  31  1F\t 57  47  2F\t 77  63  3F<\/code><\/pre>\n<p>At this point, octal is purely a curiosity. Unlike days of yore, you don&#8217;t have any distinct advantage to using octal in a program over using hexadecimal, which fits in better with today&#8217;s computer architecture. But back in the day, using octal helped code 9 bits of information or let you easily slice up a chunk of 36-bit data. It was the default lingo of the programmers and hackers of the era.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Base 8 numbers once ruled the C language. Today, they&#8217;re a relic. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=2388\">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-2388","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\/2388","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=2388"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2388\/revisions"}],"predecessor-version":[{"id":2408,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/2388\/revisions\/2408"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=2388"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=2388"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=2388"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}