{"id":149,"date":"2013-07-06T00:01:47","date_gmt":"2013-07-06T08:01:47","guid":{"rendered":"http:\/\/c-for-dummies.com\/blog\/?p=149"},"modified":"2013-07-17T21:44:16","modified_gmt":"2013-07-18T05:44:16","slug":"multiplying-without-multiplication","status":"publish","type":"post","link":"https:\/\/c-for-dummies.com\/blog\/?p=149","title":{"rendered":"Multiplying Without Multiplication"},"content":{"rendered":"<p>Long, long ago, I was an ardent assembly language programmer. I admired coding in assembly because the programs were small and fast. On the early microcomputers, that was a plus.<br \/>\n<!--more--><br \/>\nThe problem with assembly is that it takes a heinously long time to produce a workable program. That&#8217;s because you pretty much have to code everything yourself, including math functions.<\/p>\n<p>For example, when I coded in Z80 and then 8086, a multiplication operator didn&#8217;t exist. When you wanted to multiply two numbers, you had to concoct a method on your own, either by adding values or by using a bit-shift operation, which doubled values. That&#8217;s the inspiration behind this week&#8217;s code:<\/p>\n<pre class=\"screen\">\r\n#include &lt;stdio.h&gt;\r\n\r\nint main()\r\n{\r\n    int value,two,eight,ten;\r\n\r\n    printf(\"Enter an integer value: \");\r\n    scanf(\"%d\",&value);\r\n\r\n    two = value &lt;&lt; 1;\r\n    eight = value &lt;&lt; 3;\r\n    ten = two + eight;\r\n\r\n    printf(\"Ten times %d is %d.\\n\",value,ten);\r\n\r\n    return(0);\r\n}<\/pre>\n<p>Before explaining the code, copy and paste it into your editor. Build and run to ensure that it works. Here&#8217;s some sample output:<\/p>\n<p><code>Enter an integer value: 14<br \/>\nTen times 14 is 140.<\/code><\/p>\n<p>It would have been really easy to include the line <code>value*=10<\/code> in the code, which would have multiplied the original input by ten. But that doesn&#8217;t explain how us ancient Assembly coders did things.<\/p>\n<p>The key to the program&#8217;s success at multiplication is the <code>&lt;&lt;<\/code> operator, which appears at Lines 10 and 11. That&#8217;s the bit-wise shift operator. Its effect on integer values is to multiply by a power of two. That&#8217;s what happens when the bits in an integer are shifted to the left. (Shifting to the right with <code>&gt;&gt;<\/code> divides an integer by a power of two.)<\/p>\n<p>So the operation at Line 10 shifts the bits in <code>value<\/code> one position to the left. The value is effectively doubled and the result sotred in the <code>two<\/code> variable.<\/p>\n<p>The operation at Line 11 shifts the bits in <code>value<\/code> three positions to the left, which is the same as multiplying <code>value<\/code> by 2<sup>3<\/sup>, or 8. Coincidentally, that value is stored in the <code>eight<\/code> variable.<\/p>\n<p>Line 12 performs the &#8220;multiplication&#8221; by adding the value from variable <code>two<\/code> plus the value of variable <code>eight<\/code>. The end result, <code>two + eight<\/code>, equals ten times the original value. That&#8217;s the way I did things in Assembly language back in the day.<\/p>\n<p>These operations work for values up to almost the full size of a signed <code>int<\/code>, which is 2,147,483,647 on most systems. So if you specified the value 3000000000 in the program, the result gets hinkey. (You&#8217;d have to type the <code>value<\/code> variable as a <code>long<\/code> or <code>long long<\/code> to handle such huge values.)<\/p>\n<p>Oh, and this code does not compile properly under C++. That&#8217;s because in C++ the <code>&lt;&lt;<\/code> operator is used for stream input.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#8217;s possible to mathematically manipulate integers by using the bit-shift operators in C. It&#8217;s not a necessary trick, but it&#8217;s something old time programmers would appreciate. <a href=\"https:\/\/c-for-dummies.com\/blog\/?p=149\">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-149","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\/149","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=149"}],"version-history":[{"count":5,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/149\/revisions"}],"predecessor-version":[{"id":183,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/149\/revisions\/183"}],"wp:attachment":[{"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=149"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=149"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/c-for-dummies.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=149"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}