{"id":710,"date":"2020-04-13T09:23:33","date_gmt":"2020-04-13T09:23:33","guid":{"rendered":"https:\/\/computerscienced.co.uk\/site\/?page_id=710"},"modified":"2022-01-11T22:30:37","modified_gmt":"2022-01-11T22:30:37","slug":"denary-to-binary-test-4-digits","status":"publish","type":"page","link":"https:\/\/computerscienced.co.uk\/site\/binary\/denary-to-binary\/denary-to-binary-test-4-digits\/","title":{"rendered":"Denary to binary test &#8211; 4 digits"},"content":{"rendered":"\n<body onload=\"displaybinary()\">\n<style>\n#myDIV {\n  width: 100%;\n  padding: 5px 0;\n  text-align: left;\n  background-color: green;\n  color: white;\n  margin-top: 5px;\ndisplay: none;\n}\n#myDIVwrong {\n  width: 100%;\n  padding: 5px 0;\n  text-align: left;\n  background-color: red;\n  color: white;\n  margin-top: 5px;\ndisplay: none;\n}\n#myDIVhelp {\n  width: 100%;\n  padding: 5px 0;\n  text-align: left;\n  background-color: #0073aa;\n  color: white;\n  margin-top: 5px;\ndisplay: none;\n}\n.tg  {border-collapse:collapse;border-spacing:0;}\n.tg td{font-family:Arial, sans-serif;font-size:18px;padding:20px 20px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}\n.tg th{font-family:Arial, sans-serif;font-size:18px;font-weight:normal;padding:20px 20px;border-style:solid;border-width:1px;overflow:hidden;word-break:normal;border-color:black;}\n.tg .tg-1wig{font-weight:bold;background-color:#9b9b9b;text-align:left;vertical-align:top}\n.tg .tg-0lax{text-align:left;vertical-align:top}\n<\/style>\n<script>\nfunction myFunction() {\nvar userInput1 = document.getElementById(\"textInput1\").value;\nvar userInput2 = document.getElementById(\"textInput2\").value;\nvar userInput3 = document.getElementById(\"textInput3\").value;\nvar userInput4 = document.getElementById(\"textInput4\").value;\ntotal = denary;\n\n\nanswercount = 0;\ntestof8 = denary-8;\nif (testof8 >=0) {\nrightinput1=1;\n}else {\nrightinput1=0;\ntestof8 = testof8+8;\n}\ntestof4 = testof8-4;\nif (testof4 >=0) {\nrightinput2=1;\n}else {\nrightinput2=0;\ntestof4 = testof4+4;\n}\ntestof2 = testof4-2;\nif (testof2 >=0) {\nrightinput3=1;\n}else {\nrightinput3=0;\ntestof2 = testof2+2;\n}\ntestof1 = testof2-1;\nif (testof1 >=0) {\nrightinput4=1;\n}else {\nrightinput4=0;\n}\n\nif (rightinput1 == userInput1) {\nanswercount = 1;\n} else {\nanswercount = 0;\n}\nif (rightinput2 == userInput2) {\nanswercount = answercount + 1;\n}\nif (rightinput3 == userInput3) {\nanswercount = answercount + 1;\n}\nif (rightinput4 == userInput4) {\nanswercount = answercount + 1;\n}\n\nif (answercount == 4) {\n  var x = document.getElementById(\"myDIV\");\n  if (x.style.display === \"block\") {\n    x.style.display = \"none\";\n  } else {\n    x.style.display = \"block\";\n  }\nanswer()\n}else {\nvar y = document.getElementById(\"myDIVwrong\");\n  if (y.style.display === \"block\") {\n    y.style.display = \"none\";\n  } else {\n    y.style.display = \"block\";\n  }\nanswer()\n}\n}\nfunction displaybinary() {\n\nvar x = document.getElementById(\"myDIV\");\n    x.style.display = \"none\";\nvar y = document.getElementById(\"myDIVwrong\");\n    y.style.display = \"none\";\nvar z = document.getElementById(\"myDIVhelp\");\n    z.style.display = \"none\";\n\ndenary = Math.floor(Math.random() * 16);\ndocument.getElementById(\"myText\").innerHTML = denary;\n}\nfunction answer() {\ntotal = denary;\ndocument.getElementById(\"answerdisplay\").innerHTML = rightinput1;\ndocument.getElementById(\"answerdisplay2\").innerHTML = rightinput2;\ndocument.getElementById(\"answerdisplay3\").innerHTML = rightinput3;\ndocument.getElementById(\"answerdisplay4\").innerHTML = rightinput4;\ndocument.getElementById(\"answer2display\").innerHTML = rightinput1;\ndocument.getElementById(\"answer2display2\").innerHTML = rightinput2;\ndocument.getElementById(\"answer2display3\").innerHTML = rightinput3;\ndocument.getElementById(\"answer2display4\").innerHTML = rightinput4;\n}\nfunction help() {\nvar z = document.getElementById(\"myDIVhelp\");\n  if (z.style.display === \"block\") {\n    z.style.display = \"none\";\n  } else {\n    z.style.display = \"block\";\n  }\n}\nfunction moveOnMax(field,nextFieldID){\n  if(field.value.length >= field.maxLength){\n    document.getElementById(nextFieldID).focus();\n  }\n}\n<\/script>\n<div id=\"myDIVhelp\" onclick=\"help()\">\n<h2>How do I convert denary to 4 digit binary?<\/h2>\nTo convert denary (sometimes referred to as decimal) to binary we simply use a table like the one below:<br\/>\n<table border=\"1\" class=\"tg\">\n  <tr>\n    <th class=\"tg-1wig\">  8  <\/th>\n    <th class=\"tg-1wig\">  4  <\/th>\n    <th class=\"tg-1wig\">  2  <\/th>\n    <th class=\"tg-1wig\">  1  <\/th>\n  <\/tr>\n  <tr>\n    <td class=\"tg-0lax\">_<\/td>\n    <td class=\"tg-0lax\">_<\/td>\n    <td class=\"tg-0lax\">_<\/td>\n    <td class=\"tg-0lax\">_<\/td>\n  <\/tr>\n<\/table>\n<p>The numbers on the top row always stay the same.  Starting from the highest number on the left we try to subtract the number on the top row from our denary number.  If we can subtract without going negative we put a 1 in that column.  If subtraction would leave a negative number we write a 0 in that column. We carry the remainder from subtraction over to the next row and repeat.<\/p>\n<p><b>Example<\/b><\/p>\nConvert the number 11 into binary.<br\/>\nFirst we put our denary number above the left hand top row number.<br\/>\n<table border=\"1\" class=\"tg\">\n<tr>\n    <th class=\"tg-1wig\">  11 <\/th>\n    <th class=\"tg-1wig\">  _  <\/th>\n    <th class=\"tg-1wig\">  _  <\/th>\n    <th class=\"tg-1wig\">  _  <\/th>\n  <\/tr>  \n<tr>\n    <th class=\"tg-1wig\">  8  <\/th>\n    <th class=\"tg-1wig\">  4  <\/th>\n    <th class=\"tg-1wig\">  2  <\/th>\n    <th class=\"tg-1wig\">  1  <\/th>\n  <\/tr>\n  <tr>\n    <td class=\"tg-0lax\">  _  <\/td>\n    <td class=\"tg-0lax\">  _  <\/td>\n    <td class=\"tg-0lax\">  _  <\/td>\n    <td class=\"tg-0lax\">  _  <\/td>\n  <\/tr>\n<\/table>\n<p>We can subtract 8 from 11 without leaving a negative number so we put a 1 below the 8 and the remainder from our subtraction of 11 &#8211; 8 which is 3 goes to the top of the next column.<\/p>\n<table border=\"1\" class=\"tg\">\n<tr>\n    <th class=\"tg-1wig\">  11 <\/th>\n    <th class=\"tg-1wig\">  3  <\/th>\n    <th class=\"tg-1wig\">  _  <\/th>\n    <th class=\"tg-1wig\">  _  <\/th>\n  <\/tr>  \n<tr>\n    <th class=\"tg-1wig\">  8  <\/th>\n    <th class=\"tg-1wig\">  4  <\/th>\n    <th class=\"tg-1wig\">  2  <\/th>\n    <th class=\"tg-1wig\">  1  <\/th>\n  <\/tr>\n  <tr>\n    <td class=\"tg-0lax\">  1  <\/td>\n    <td class=\"tg-0lax\">  _  <\/td>\n    <td class=\"tg-0lax\">  _  <\/td>\n    <td class=\"tg-0lax\">  _  <\/td>\n  <\/tr>\n<\/table>\n<p>We do the same for our second column.  We cannot subtract 4 from 3 as it would leave a negative number so we write a 0 in the row below the 4 and carry our remainder of 3 over to the next column.<\/p>\n<table border=\"1\" class=\"tg\">\n<tr>\n    <th class=\"tg-1wig\">  11 <\/th>\n    <th class=\"tg-1wig\">  3  <\/th>\n    <th class=\"tg-1wig\">  3  <\/th>\n    <th class=\"tg-1wig\">  _  <\/th>\n  <\/tr>  \n<tr>\n    <th class=\"tg-1wig\">  8  <\/th>\n    <th class=\"tg-1wig\">  4  <\/th>\n    <th class=\"tg-1wig\">  2  <\/th>\n    <th class=\"tg-1wig\">  1  <\/th>\n  <\/tr>\n  <tr>\n    <td class=\"tg-0lax\">  1  <\/td>\n    <td class=\"tg-0lax\">  0  <\/td>\n    <td class=\"tg-0lax\">  _  <\/td>\n    <td class=\"tg-0lax\">  _  <\/td>\n  <\/tr>\n<\/table>\n<p>We can subtract 2 from 3 so we put a 1 in the row below the 2 and carry our remainder of 1 over to the top of the 1 column<\/p>\n<table border=\"1\" class=\"tg\">\n<tr>\n    <th class=\"tg-1wig\">  11 <\/th>\n    <th class=\"tg-1wig\">  3  <\/th>\n    <th class=\"tg-1wig\">  3  <\/th>\n    <th class=\"tg-1wig\">  1  <\/th>\n  <\/tr>  \n<tr>\n    <th class=\"tg-1wig\">  8  <\/th>\n    <th class=\"tg-1wig\">  4  <\/th>\n    <th class=\"tg-1wig\">  2  <\/th>\n    <th class=\"tg-1wig\">  1  <\/th>\n  <\/tr>\n  <tr>\n    <td class=\"tg-0lax\">  1  <\/td>\n    <td class=\"tg-0lax\">  0  <\/td>\n    <td class=\"tg-0lax\">  1  <\/td>\n    <td class=\"tg-0lax\">  _  <\/td>\n  <\/tr>\n<\/table>\n<p>We can take 1 away from 1 without leaving a negative value so we put a 1 in the row below the 1 to complete our conversion.  By the last column we should have 0 left.  Keep in mind that 4 digit binary will only cover denary numbers up to 15.  Any larger numbers (up to 255) will need to be calculated using an 8 bit grid.<\/p>\n<p>11 in binary is 1011<\/p>\n<table border=\"1\" class=\"tg\">\n<tr>\n    <th class=\"tg-1wig\">  11 <\/th>\n    <th class=\"tg-1wig\">  3  <\/th>\n    <th class=\"tg-1wig\">  3  <\/th>\n    <th class=\"tg-1wig\">  1  <\/th>\n  <\/tr>  \n<tr>\n    <th class=\"tg-1wig\">  8  <\/th>\n    <th class=\"tg-1wig\">  4  <\/th>\n    <th class=\"tg-1wig\">  2  <\/th>\n    <th class=\"tg-1wig\">  1  <\/th>\n  <\/tr>\n  <tr>\n    <td class=\"tg-0lax\">  1  <\/td>\n    <td class=\"tg-0lax\">  0  <\/td>\n    <td class=\"tg-0lax\">  1  <\/td>\n    <td class=\"tg-0lax\">  1  <\/td>\n  <\/tr>\n<\/table>\n<\/div>\n<p>Convert the following denary (decimal) number to its 4 bit binary equivalent:<\/p>\n<h2><span id=\"myText\"><\/span><\/h2>\n<p>Enter your answer: <br><input type=\"text\" id=\"textInput1\" onfocus=\"this.value=''\" maxlength = \"1\" style=\"width: 50px;\" onkeyup=\"moveOnMax(this,'textInput2')\"\/>  <input type=\"text\" id=\"textInput2\" onfocus=\"this.value=''\" maxlength = \"1\" style=\"width: 50px;\" onkeyup=\"moveOnMax(this,'textInput3')\"\/>  <input type=\"text\" id=\"textInput3\" onfocus=\"this.value=''\" maxlength = \"1\" style=\"width: 50px;\" onkeyup=\"moveOnMax(this,'textInput4')\"\/>  <input type=\"text\" id=\"textInput4\" onfocus=\"this.value=''\" maxlength = \"1\" style=\"width: 50px;\"\/><\/p>\n<button onclick=\"myFunction()\">Submit your answer<\/button>\n<button onclick=\"displaybinary()\">Click to test again<\/button>\n<button onclick=\"help()\">Help<\/button>\n<div id=\"myDIV\">\n<h3>That is right!<\/h3>\n<h4>The right answer is <span id=\"answerdisplay\"><\/span><span id=\"answerdisplay2\"><\/span><span id=\"answerdisplay3\"><\/span><span id=\"answerdisplay4\"><\/span><\/h4>\n\n<\/div>\n<div id=\"myDIVwrong\">\n<h3>That is not correct<\/h3>\n<h4>The right answer is <span id=\"answer2display\"><\/span><span id=\"answer2display2\"><\/span><span id=\"answer2display3\"><\/span><span id=\"answer2display4\"><\/span><\/h4>\n<\/div>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>How do I convert denary to 4 digit binary? To convert denary (sometimes referred to as decimal) to binary we&#8230;<\/p>\n","protected":false},"author":1,"featured_media":2740,"parent":879,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"inline_featured_image":false,"disable_featured_image":true,"footnotes":""},"class_list":["post-710","page","type-page","status-publish","has-post-thumbnail","hentry"],"blog_post_layout_featured_media_urls":{"thumbnail":["https:\/\/computerscienced.co.uk\/site\/wp-content\/uploads\/2021\/10\/undraw_exams_g4ow-1.svg",150,150,true],"full":["https:\/\/computerscienced.co.uk\/site\/wp-content\/uploads\/2021\/10\/undraw_exams_g4ow-1.svg",982,803,false]},"categories_names":null,"comments_number":"0","_links":{"self":[{"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/pages\/710","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/comments?post=710"}],"version-history":[{"count":18,"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/pages\/710\/revisions"}],"predecessor-version":[{"id":1722,"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/pages\/710\/revisions\/1722"}],"up":[{"embeddable":true,"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/pages\/879"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/media\/2740"}],"wp:attachment":[{"href":"https:\/\/computerscienced.co.uk\/site\/wp-json\/wp\/v2\/media?parent=710"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}