{"id":2933,"date":"2018-10-08T09:07:21","date_gmt":"2018-10-08T09:07:21","guid":{"rendered":"https:\/\/blog.paranoidprofessor.com\/?p=2933"},"modified":"2018-10-08T09:07:21","modified_gmt":"2018-10-08T09:07:21","slug":"a-binary-converter","status":"publish","type":"post","link":"https:\/\/blog.paranoidprofessor.com\/index.php\/2018\/10\/08\/a-binary-converter\/","title":{"rendered":"A binary converter"},"content":{"rendered":"<p>I&#8217;m a hobbyist programmer and I program mainly in python, but am learning Ruby,which is similar to python. In this post I&#8217;m going to show you a simple python program:<\/p>\n<pre class=\"brush: plain; title: ; notranslate\" title=\"\">\r\n#program loop\r\nwhile True:\r\n#input\r\n    userinput = int(input('Which decimal integer would you like converted to binary?'))\r\n#processing\r\n    if userinput == 0:\r\n        print('0')\r\n        continue\r\n    else:\r\n        binary = ''\r\n        while int(userinput) &amp;gt; 0:\r\n            digit = str(userinput % 2)\r\n            userinput = userinput \/ 2\r\n            binary = digit + binary\r\n    #output\r\n    print(binary)\r\n<\/pre>\n<p>Can you guess what this does? If you guessed that it converts decimal numbers to binary numbers, you&#8217;re correct. I&#8217;ll be explaining how it does that now. If you want to try this program yourself you have to use <a href=\"https:\/\/www.python.org\/downloads\/\">python 3<\/a>. I will be breaking the code up into little pieces and explain what they do.<\/p>\n<h2><strong>INPUT<\/strong><\/h2>\n<p>First lets start with the part marked as input. All it really does is ask you the question: &#8220;Which decimal integer would you like converted to binary?&#8221; and sets the variable &#8220;userinput&#8221; to the response of the user to that question.<\/p>\n<h2><strong>PROCESSING<\/strong><\/h2>\n<p>1. It checks if the value &#8220;userinput&#8221; is equal to zero.\u00a0\u00a0\u00a0 if userinput == 0:<\/p>\n<p style=\"padding-left: 30px\">1a.The program prints 0.\u00a0\u00a0print(&#8216;0&#8217;)<\/p>\n<p style=\"padding-left: 30px\">1b.The program skips to the next loop. \u00a0\u00a0 continue<\/p>\n<p>2. It does the following steps if &#8220;userinput&#8221; wasn&#8217;t equal to zero.\u00a0\u00a0\u00a0 else:<\/p>\n<p style=\"padding-left: 30px\">2a. It prepares following steps by creating a variable called &#8220;binary&#8221; and setting it&#8217;s value to a blank string.\u00a0\u00a0\u00a0 binary = &#8216; &#8216;<\/p>\n<p style=\"padding-left: 30px\">2b. It will loop these statements as long as &#8220;userinput&#8221; is positive.\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 while int(userinput) &gt; 0:<\/p>\n<p style=\"padding-left: 60px\">~this statement sets the variable &#8220;digit&#8221; to the mod (the remainder) but as a string.\u00a0\u00a0\u00a0 digit = str(userinput % 2)<\/p>\n<p style=\"padding-left: 60px\">~this step divides &#8220;userinput&#8221; by 2.\u00a0\u00a0\u00a0 userinput = userinput \/ 2<\/p>\n<p style=\"padding-left: 60px\">~the last step of the loop puts the variable &#8220;digit&#8221; in front of binary.\u00a0\u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0 \u00a0\u00a0 binary = digit + binary<\/p>\n<h2><strong>OUTPUT<\/strong><\/h2>\n<p>The output step doesn&#8217;t do much. It just prints the &#8220;binary&#8221; value.<\/p>\n<hr \/>\n<h2><strong>TERMS<\/strong><\/h2>\n<p>variable: an interchangeable value<\/p>\n<p>algorithm: a sequence of commands<\/p>\n<p>function: a kind of command<\/p>\n<h2><strong>PYTHON COMMANDS<\/strong><\/h2>\n<p>#something &#8211;&gt; This is a comment. It will not be read by python. You can obviously put in anything for &#8220;something&#8221;<\/p>\n<p>while a == b: &#8211;&gt;This is a loop it will loop through the algorithm that come after it as long as the statement after the while is true.<\/p>\n<p>examplevariable = 1 &#8211;&gt;This command sets a value to a variable.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;m a hobbyist programmer and I program mainly in python, but am learning Ruby,which is similar to python. In this post I&#8217;m going to show you a simple python program: Can you guess what this does? If you guessed that &hellip; <a href=\"https:\/\/blog.paranoidprofessor.com\/index.php\/2018\/10\/08\/a-binary-converter\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":35,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[20,100],"tags":[36],"_links":{"self":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2933"}],"collection":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/users\/35"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/comments?post=2933"}],"version-history":[{"count":7,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2933\/revisions"}],"predecessor-version":[{"id":3055,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/posts\/2933\/revisions\/3055"}],"wp:attachment":[{"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/media?parent=2933"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/categories?post=2933"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.paranoidprofessor.com\/index.php\/wp-json\/wp\/v2\/tags?post=2933"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}