View the code
Submit
Binary to denary Enter an 8 bit binary number. Press submit for conversion.
Clear
Converting between denary and binary
Denary to binary Enter a number between 0 and 255. Press submit for conversion.
var number = entryBox.innerHTML
If the number is equal to or greater than 64 (the value of bit 7)then bitSeven is given the value of 1. It was already given the value of 0 and so an else statement is not needed. 64 is deducted from the number as bitSeven = 1.
if(number > 255 {alert('Number must be between 0 and 255');  entryBox.innerHTML = ""; return; }
Declare variables for the eight bits. All set to 0.
The variables are now displayed with spaces between them.
Denary to binary
The variable 'number' is set to the number entered by the user.
Move your mouse pointer over the statements to read an explanation.
The number is checked to see if it is greater than 255.If it is an error message is displayed. The entry box is cleared of the user input. The function is halted.
if(number >= 128) { bitEight = 1; number = number - 128; }
var bitEight = 0 … var bitOne = 0
Similar blocks of code to bitOne.
if(number >= 64) { bitSeven = 1; number = number - 64; }
If the number is equal to or greater than 128 (the value of bit 8)then bitEight is given the value of 1. It was already given the value of 0 and so an else statement is not needed. 128 is deducted from the number as bitEight = 1.
displayBox.innerHTML = 'bitEight + " " + bitSeven + " " + bitSix…bitOne'
var total = (binary[0] * 128] + (binary[1] * 64) + (binary[3] * 32) …………. + (binary[7] * 1);
The length is checked to see if it is equal to 8.If it is not equal to 8 an error message is displayed. The entry box is cleared of the user input. The function is halted.
An array called 'binary' is declared to hold the 8 digits entered. A 'for' loop, starting at 0 and ending at 7, checks each digit and stores them in the array. eg number.substr(0,1) will clip off the first digit and store it in binary[0]. 
The variable 'total' is now displayed.
var binary = new Array(); for (var i = 0; i < 8; i++) { binary[i] = number.substr(i, 1);  }
displayBox.innerHTML = total;
var length = number.length;
The variable 'length' is set to the length of the string entered.
var number = enterBox.innerHTML;
Binary to denary
Each binary digit is now multiplied by its place value. binary[0] - the left-most digit is multiplied by 128 and binary[7], the right-most digit by 1. The sum of these is stored in the variable 'total'.
if(length != 8 {alert('Must be an 8 bit binary number.');  entryBox.innerHTML = ""; return; }