{"id":592,"date":"2014-03-15T00:01:07","date_gmt":"2014-03-15T08:01:07","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=592"},"modified":"2014-03-08T08:58:26","modified_gmt":"2014-03-08T16:58:26","slug":"getting-shifty","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=592","title":{"rendered":"Getting Shifty"},"content":{"rendered":"<p>Something that you can do in the C language that C++ programmers cannot is employ the shift operators. These operators, <code>&lt;&lt;<\/code> and <code>&gt;&gt;<\/code>, serve as I\/O operators in C++. In C, however, they can be used to manipulate values at the bit level in C, which is part of the C language&#8217;s mid-level heritage.<br \/>\n<!--more--><br \/>\nWhen it comes to peeling information from a byte of storage, nothing beats the C language&#8217;s low-level operators. In concert with the bitwise logical operators: <code>|<\/code>, <code>&<\/code>, and <code>^<\/code>, you can use <code>&lt;&lt;<\/code> and <code>&gt;&gt;<\/code> to manipulate data at the computer equivalent of the microscopic level. You can also do math.<\/p>\n<p>Back in my early programming days, I was an enthusiastic fan of Assembly language, which allows you to directly program a computer&#8217;s processor. Assembly has no functions. It has no operators. Everything you do in an Assembly program &#8212; <em>everything<\/em> &#8212; must be coded from scratch. That includes the basic math operations multiplication and division.<\/p>\n<blockquote><p>All processors handle basic addition, subtraction, and increment\/decrement operations. Today&#8217;s processors have instructions for multiplication or division, but that wasn&#8217;t common back in the early days.<\/p><\/blockquote>\n<p>The following code demonstrates how multiplication can be done in the C language without using the <code>*<\/code> multiplication operator. Just as with programming a processor directly by using Assembly language, the operations performed in this code are all at the bit level:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int x,bytwo,byeight,byten;\r\n\r\n    printf(\"Type an integer: \");\r\n    scanf(\"%d\",&x);\r\n\r\n    bytwo = x &lt;&lt; 1;\r\n    byeight = x &lt;&lt; 3;\r\n    byten = bytwo + byeight;\r\n\r\n    printf(\"%d x 10 = %d\\n\",x,byten);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Here&#8217;s a sample run:<\/p>\n<p><code>Type an integer: 489<br \/>\n489 x 10 = 4890<\/code><\/p>\n<p>It even works on negative numbers:<\/p>\n<p><code>Type an integer: -30<br \/>\n-30 x 10 = -300<\/code><\/p>\n<p>The left-shift operator <code>&lt;&lt;<\/code> is used twice in the above code, first at Line 10 and then at Line 11.<\/p>\n<p><code>bytwo = x &lt;&lt; 1;<\/code><\/p>\n<p>The effect of this code is to double the value in variable <code>x<\/code>. The bits are shifted one notch to the left, which in binary terms multiplies that value by two. This bit-shift operation is extremely fast because the processor can manipulate data directly in that manner. You could almost say the processor <em>enjoys<\/em> performing that operation.<\/p>\n<p><code>byeight = x &lt;&lt; 3;<\/code><\/p>\n<p>This statement shifts the value in <code>x<\/code> three notches to the left at the binary level. The effect is to multiply the value by 8. Conveniently, 2<sup>3<\/sup> is 8, which is why the value 3 is used.<\/p>\n<p>With the original value <code>x<\/code> multiplied by 2 and stored in variable <code>bytwo<\/code>, and the original value of <code>x<\/code> multiplied by 8 and stored in the variable <code>byeight<\/code>, the result of <code>bytwo<\/code> and <code>byeight<\/code> would be to multiply the value <code>x<\/code> by 10. That&#8217;s what the program does. And that&#8217;s exactly how the operation would have been performed at the low level in Assembly language.<\/p>\n<p>The only problem you get into is with very large integers, in which case the <code>&lt;&lt;<\/code> operator shifts the left-most bit in a value off into oblivion. But for small values, this trick is quite effective.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Back in the old days, computer memory was tight and processors were slow. If you wanted to code quickly, you had to eschew higher level math functions and get down and dirty with the processor. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=592\">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-592","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\/592","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=592"}],"version-history":[{"count":7,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/592\/revisions"}],"predecessor-version":[{"id":615,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/592\/revisions\/615"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=592"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=592"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=592"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}