BITS_AND_BYTES


The single web page application featured in this tutorial web page allows the end user to click switches which each flip a single uniquely corresponding light bulb from “OFF” to “ON” and vice versa and, also, to click a BINARY_TO_DECIMAL button which computes the binary number represented by the light bulb array configuration at the time that button is clicked. Algebraic equations which are used to convert the base-two integer value represented by the sequence of eight light bulbs to that base-two integer’s base-ten equivalent value are displayed at the bottom of this web page. (Clicking the RESET button will return this web page to its initial state such that each one of the eight light bulbs is set to “OFF” and all buttons on this web page are set to visible).

The binary integer value 0 is an abstract representation of the relatively physical “OFF” (insufficiently high voltage) light bulb state.

The binary integer value 1 is an abstract representation of the relatively physical “ON” (sufficiently high voltage) light bulb state.

Each one of the eight light bulbs displayed in the table below represents a particular power of two.

(N in the table below represents a nonnegative integer value no larger than seven).

While light_bulb_N is in its “ON” state, light_bulb_N represents two multiplied by itself N times.

While light_bulb_N is in its “OFF” state, light_bulb_N represents zero.

The nonnegative decimal integer value which the sequence of eight light bulbs represents is computed by adding up the nonnegative decimal integer values which each of those eight light bulbs represents.

The largest decimal integer value which the sequence of eight light bulbs can represent is 255.

The smallest decimal integer value which the sequence of eight light bulbs can represents is 0.

The total number of unique states which the sequence of eight light bulbs can represent is 256.

The largest binary integer value which any one of the eight light bulbs can represent is 1.

The smallest binary integer value which any one of the eight light bulbs can represent is 0.

The total number of unqiue states which any one of the eight light bulbs can represent is 2.

The rightmost light bulb has an N value of 0.

The rightmost light bulb represents the “lowest order bit” in the byte which the sequence of eight light bulb represents.

lowest_order_bit(ON) = 1 * (2 ^ 0) = 1 * 1 = 1.

lowest_order_bit(OFF) = 0 * (2 ^ 0) = 0 * 1 = 0.

The leftmost light bulb has an N value of 7.

The leftmost light bulb represents the “highest order bit” in the byte which the sequence of eight light bulb represents.

highest_order_bit(ON) = 1 * (2 ^ 7) = 1 * 2 * 2 * 2 * 2 * 2 * 2 * 2 = 128.

highest_order_bit(OFF) = 0 * (2 ^ 7) = 0 * 2 * 2 * 2 * 2 * 2 * 2 * 2 = 0.

To view hidden text inside each of the preformatted text boxes below, scroll horizontally.

BIT := abbreviation("binary digit") = OR(0,1). // A bit represents one of two possible unique states at a time (and those two states are the integers 0 and 1).
BYTE := abbreviation("binary term") = BIT[8]. // A byte represents a sequence of eight bits.

SOFTWARE_APPLICATION_COMPONENTS


Hyper-Text-Markup-Language_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/binary_to_decimal.html

Cascading-Style-Sheet_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/karbytes_aesthetic.css

JavaScript_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/binary_to_decimal.js

image_link: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/light_bulb_off.png

image_link: https://raw.githubusercontent.com/KARLINA_OBJECT_summer_2023_starter_pack/main/light_bulb_on.png


Hyper-Text-Markup-Language Code


The following Hyper-Text-Markup-Language (HTML) code defines the user interface component of the BINARY_TO_DECIMAL web page application. Copy the HTML code from the source code file which is linked below into a text editor and save that file as binary_to_decimal.html. Use a web browser such as Firefox to open that HTML file (and ensure that the JavaScript and Cascading-Style-Sheet files are in the same file directory as the HTML file).

Note that the contents of the HTML file are not displayed in a preformatted text box on this web page due to the fact that the WordPress server makes no distinction between HTML code which is encapsulated inside of a preformatted text box and WordPress web page source code.

Hyper-Text-Markup-Language_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/binary_to_decimal.html

image_link: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/binary_to_decimal_html_code_screenshot.png



Cascading-Style-Sheet Code


The following Cascading-Style-Sheet (CSS) code defines a stylesheet which customizes the appearance of interface components of the BINARY_TO_DECIMAL web page application. Copy the CSS code from the source code file which is linked below into a text editor and save that file as karbytes_aesthetic.css.

Cascading-Style-Sheet_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/karbytes_aesthetic.css


/**
 * file: karbytes_aesthetic.css
 * type: Cascading-Style-Sheet
 * date: 10_JULY_2023
 * author: karbytes
 * license: PUBLIC_DOMAIN
 */

/** Make the page background BLACK, the text orange and monospace, and the page content width 800 pixels or less. */
body {
 	background: #000000;
 	color: #ff9000;
 	font-family: monospace;
 	font-size: 16px;
 	padding: 10px;
 	width: 800px;
}

/** Make input elements and select elements have an orange rounded border, a BLACK background, and orange monospace text. */
input, select {
	background: #000000;
	color: #ff9000;
	border-color: #ff9000;
	border-width: 1px;
	border-style: solid;
	border-radius: 5px;
	padding: 10px;
	appearance: none;  
 	font-family: monospace;
 	font-size: 16px;
}

/** Invert the text color and background color of INPUT and SELECT elements when the cursor (i.e. mouse) hovers over them. */
input:hover, select:hover {
	background: #ff9000;
	color: #000000;
}

/** Make table data borders one pixel thick and CYAN. Give table data content 10 pixels in padding on all four sides. */
td {
	color: #00ffff;
	border-color: #00ffff;
	border-width: 1px;
	border-style: solid;
	padding: 10px;
}

/** Set the text color of elements whose identifier (id) is "output" to CYAN. */
#output {
	color: #00ffff;
}

/** Set the text color of elements whose class is "console" to GREEN and make the text background of those elements BLACK. */
.console {
	color: #00ff00;
	background: #000000;
}

JavaScript Code


The following JavaScript (JS) code defines the functions which control the behavior of the BINARY_TO_DECIMAL web page application. Copy the JS code from the source code file which is linked below into a text editor and save that file as binary_to_decimal.js.

JavaScript_file: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/binary_to_decimal.js


/**
 * file: binary_to_decimal.js
 * type: JavaScript
 * date: 10_JULY_2023
 * author: karbytes
 * license: PUBLIC_DOMAIN
 */

/**
 * Display a time stamped message inside of the div element whose id is "output".
 * 
 * Set each of the eight light bulbs to "OFF" rather rather than to "ON".
 * 
 * Set each of the eight binary digit values to zero (0) rather than to one (1).
 * 
 * Set each of the buttons on the web page to visible rather than to hidden.
 */
function initialize_application() {
    try {
        const time_point = Date.now(), p0 = ('<' + 'p' + '>'), p1 = ('<' + '/' + 'p' + '>');
        let light_bulb_off = '<' + 'img src="light_bulb_off.png" width="60"' + '>', i = 0;
        const message = "The initialize_application() function was called at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).";
        console.log(message);
        document.getElementById("output").innerHTML = p0 + message + p1;
        for (i = 0; i < 8; i += 1) {
            document.getElementById("light_bulb_" + i).innerHTML = light_bulb_off;
            document.getElementById("bit_" + i).innerHTML = 0;
            document.getElementById("switch_" + i).style.display = "block";
        }
        document.getElementById("binary_to_decimal_button").style.display = "inline";
    }
    catch(exception) {
        console.log("An exception to expected functioning occurred during the runtime of the JavaScript function named initialize_application(): " + exception);
    }
}

/**
 * Change the binary state represented by the Nth light bulb in the sequence of 8 light bulbs to its only alternative state.
 * 
 * If the Nth bit value is set to 0, the Nth light bulb state is assumed to be "OFF".
 * 
 * If the Nth bit value is set to 1, the Nth light bulb state is assumed to be "ON".
 * 
 * When the switch for the Nth light bulb is clicked, the following will occur:
 * 
 * If the Nth bit is set to 0, the Nth bit will be set to 1 and the Nth light bulb state will be set to "ON".
 * 
 * If the Nth bit is set to 1, the Nth bit will be set to 0 and the Nth light bulb state will be set to "OFF".
 * 
 * @param {Number} N is assumed to be a base-ten integer no smaller than 0 and no larger than 7.
 */
function binary_switch(N) {
    try {
        const p0 = ('<' + 'p' + '>'), p1 = ('<' + '/' + 'p' + '>');
        let bit = undefined, light_bulb_image = undefined, light_bulb_off = '<' + 'img src="light_bulb_off.png" width="60"' + '>', light_bulb_on = '<' + 'img src="light_bulb_on.png" width="60"' + '>';
        bit = parseInt(document.getElementById("bit_" + N).innerHTML);
        if (bit === 0) {
            document.getElementById("bit_" + N).innerHTML = 1;
            document.getElementById("light_bulb_" + N).innerHTML = light_bulb_on;
        }
        else {
            document.getElementById("bit_" + N).innerHTML = 0;
            document.getElementById("light_bulb_" + N).innerHTML = light_bulb_off; 
        }
    }
    catch(exception) {
        console.log("An exception to expected functioning occurred during the runtime of the JavaScript function named binary_switch(N): " + exception);
    }
}

/**
 * Change the binary state represented by the 7th (i.e. seventh) light bulb in the array of 8 light bulbs to its only alternative state.
 * 
 * Note that the 7th light bulb represents the highest order bit and is the leftmost light bulb in the array.
 * 
 * If the 7th bit value is set to 0, the 7th light bulb state is assumed to be "OFF".
 * 
 * If the 7th bit value is set to 1, the 7th light bulb state is assumed to be "ON".
 * 
 * When the switch for the 7th light bulb is clicked, the following will occur:
 * 
 * If the 7th bit is set to 0, then the Nth bit will be set to 1 and the 7th light bulb state will be set to "ON".
 * 
 * Otherwise, the 7th bit will be set to 0 and the 7th light bulb state will be set to "OFF".
 */
function switch_7() { 
    const time_point = Date.now();
    console.log("The switch_7 button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).");
    binary_switch(7); 
}

/**
 * Change the binary state represented by the 6th (i.e. sixth) light bulb in the array of 8 light bulbs to its only alternative state.
 * 
 * If the 6th bit value is set to 0, the 6th light bulb state is assumed to be "OFF".
 * 
 * If the 6th bit value is set to 1, the 6th light bulb state is assumed to be "ON".
 * 
 * When the switch for the 6th light bulb is clicked, the following will occur:
 * 
 * If the 6th bit is set to 0, then the 6th bit will be set to 1 and the 6th light bulb state will be set to "ON".
 * 
 * Otherwise, the 6th bit will be set to 0 and the 6th light bulb state will be set to "OFF".
 */
function switch_6() { 
    const time_point = Date.now();
    console.log("The switch_6 button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).");
    binary_switch(6); 
}

/**
 * Change the binary state represented by the 5th (i.e. fifth) light bulb in the array of 8 light bulbs to its only alternative state.
 * 
 * If the 5th bit value is set to 0, the 5th light bulb state is assumed to be "OFF".
 * 
 * If the 5th bit value is set to 1, the 5th light bulb state is assumed to be "ON".
 * 
 * When the switch for the 5th light bulb is clicked, the following will occur:
 * 
 * If the 5th bit is set to 0, then the 5th bit will be set to 1 and the 5th light bulb state will be set to "ON".
 * 
 * Otherwise, the 5th bit will be set to 0 and the 5th light bulb state will be set to "OFF".
 */
function switch_5() { 
    const time_point = Date.now();
    console.log("The switch_5 button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).");
    binary_switch(5); 
}

/**
 * Change the binary state represented by the 4th (i.e. fourth) light bulb in the array of 8 light bulbs to its only alternative state.
 * 
 * If the 4th bit value is set to 0, the 4th light bulb state is assumed to be "OFF".
 * 
 * If the 4th bit value is set to 1, the 4th light bulb state is assumed to be "ON".
 * 
 * When the switch for the 4th light bulb is clicked, the following will occur:
 * 
 * If the 4th bit is set to 0, then the 4th bit will be set to 1 and the 4th light bulb state will be set to "ON".
 * 
 * Otherwise, the 4th bit will be set to 0 and the 4th light bulb state will be set to "OFF".
 */
function switch_4() { 
    const time_point = Date.now();
    console.log("The switch_4 button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).");
    binary_switch(4); 
}

/**
 * Change the binary state represented by the 3rd (i.e. third) light bulb in the array of 8 light bulbs to its only alternative state.
 * 
 * If the 3rd bit value is set to 0, the 3rd light bulb state is assumed to be "OFF".
 * 
 * If the 3rd bit value is set to 1, the 3rd light bulb state is assumed to be "ON".
 * 
 * When the switch for the 3rd light bulb is clicked, the following will occur:
 * 
 * If the 3rd bit is set to 0, then the 3rd bit will be set to 1 and the 3rd light bulb state will be set to "ON".
 * 
 * Otherwise, the 3rd bit will be set to 0 and the 3rd light bulb state will be set to "OFF".
 */
function switch_3() { 
    const time_point = Date.now();
    console.log("The switch_3 button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).");
    binary_switch(3); 
}

/**
 * Change the binary state represented by the 2nd (i.e. second) light bulb in the array of 8 light bulbs to its only alternative state.
 * 
 * If the 2nd bit value is set to 0, the 2nd light bulb state is assumed to be "OFF".
 * 
 * If the 2nd bit value is set to 1, the 2nd light bulb state is assumed to be "ON".
 * 
 * When the switch for the 2nd light bulb is clicked, the following will occur:
 * 
 * If the 2nd bit is set to 0, then the 2nd bit will be set to 1 and the 2nd light bulb state will be set to "ON".
 * 
 * Otherwise, the 2nd bit will be set to 0 and the 2nd light bulb state will be set to "OFF".
 */
function switch_2() { 
    const time_point = Date.now();
    console.log("The switch_2 button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).");
    binary_switch(2); 
}

/**
 * Change the binary state represented by the 1st (i.e. first) light bulb in the array of 8 light bulbs to its only alternative state.
 * 
 * If the 1st bit value is set to 0, the 1st light bulb state is assumed to be "OFF".
 * 
 * If the 1st bit value is set to 1, the 1st light bulb state is assumed to be "ON".
 * 
 * When the switch for the 1st light bulb is clicked, the following will occur:
 * 
 * If the 1st bit is set to 0, then the 1st bit will be set to 1 and the 1st light bulb state will be set to "ON".
 * 
 * Otherwise, the 1st bit will be set to 0 and the 1st light bulb state will be set to "OFF".
 */
function switch_1() { 
    const time_point = Date.now();
    console.log("The switch_1 button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).");
    binary_switch(1); 
}

/**
 * Change the binary state represented by the 0th (i.e. zeroth) light bulb in the array of 8 light bulbs to its only alternative state.
 * 
 * Note that the 0th light bulb represents the lowest order bit and is the rightmost light bulb in the array.
 * 
 * If the 0th bit value is set to 0, the 0th light bulb state is assumed to be "OFF".
 * 
 * If the 0th bit value is set to 1, the 0th light bulb state is assumed to be "ON".
 * 
 * When the switch for the 1st light bulb is clicked, the following will occur:
 * 
 * If the 0th bit is set to 0, then the 0th bit will be set to 1 and the 1st light bulb state will be set to "ON".
 * 
 * Otherwise, the 0th bit will be set to 0 and the 0th light bulb state will be set to "OFF".
 */
function switch_0() { 
    const time_point = Date.now();
    console.log("The switch_0 button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).");
    binary_switch(0); 
}

/**
 * Hide all buttons except for the RESET button.
 * 
 * Convert the input binary number (represented by the current configuration of eight light bulbs)
 * to its logically equivalent decimal output number and 
 * display the arithmetic steps used to convert the input value to the output value inside the div element whose id is "output".
 */
function binary_to_decimal() {
    try {
        const time_point = Date.now(), p0 = '<' + 'p' + '>', p1 = '<' + '/' + 'p' + '>', s0 = '<' + 'span class="console"' + '>', s1 = '<' + '/' + 'span' + '>';
        let i = 0, binary_digits_string = "", decimal_output_number = 0, decimal_term_value = 0,  arithmetic_steps = "", message = "";
        message = "The BINARY_TO_DECIMAL button was clicked at time: " + time_point + " milliseconds since 01_JANUARY_1970 00:00:00 (Coordinated Universal Time (UTC)).";
        console.log(message);
        document.getElementById("output").innerHTML += p0 + message + p1;
        for (i = 0; i < 8; i += 1) document.getElementById("switch_" + i).style.display = "none";
        document.getElementById("binary_to_decimal_button").style.display = "none";
        for (i = 7; i > -1; i -= 1) binary_digits_string += document.getElementById("bit_" + i).innerHTML;
        console.log("The input binary digit sequence is " + binary_digits_string + ".");
        for (i = 7; i > -1; i -= 1) {
            decimal_term_value = parseInt(binary_digits_string[i]) * Math.pow(2, 7 - i);
            decimal_output_number += decimal_term_value;
            
        }
        console.log("The output decimal digit sequence is " + decimal_output_number + ".");
        arithmetic_steps += p0 + "binary_input: " + s0 + binary_digits_string + s1 + "." + p1;
        arithmetic_steps += p0 + "decimal_output: " + decimal_output_number + "." + p1;
        arithmetic_steps += p0 + "arithmetic_steps: " + p1;
        arithmetic_steps += p0 + decimal_output_number + " = " + p1;
        arithmetic_steps += p0 + "(" + s0 + binary_digits_string[0] + s1 + " * (2 ^ 7)) +" + p1;
        arithmetic_steps += p0 + "(" + s0 + binary_digits_string[1] + s1 + " * (2 ^ 6)) +" + p1;
        arithmetic_steps += p0 + "(" + s0 + binary_digits_string[2] + s1 + " * (2 ^ 5)) +" + p1;
        arithmetic_steps += p0 + "(" + s0 + binary_digits_string[3] + s1 + " * (2 ^ 4)) +" + p1;
        arithmetic_steps += p0 + "(" + s0 + binary_digits_string[4] + s1 + " * (2 ^ 3)) +" + p1;
        arithmetic_steps += p0 + "(" + s0 + binary_digits_string[5] + s1 + " * (2 ^ 2)) +" + p1;
        arithmetic_steps += p0 + "(" + s0 + binary_digits_string[6] + s1 + " * (2 ^ 1)) +" + p1;
        arithmetic_steps += p0 + "(" + s0 + binary_digits_string[7] + s1 + " * (2 ^ 0)) = " + p1;
        arithmetic_steps += p0 + "(" + s0 + binary_digits_string[0] + s1 + " * 128) +" + p1;
        arithmetic_steps += p0 + "(" + s0 + binary_digits_string[1] + s1 + " * 64) +" + p1;
        arithmetic_steps += p0 + "(" + s0 + binary_digits_string[2] + s1 + " * 32) +" + p1;
        arithmetic_steps += p0 + "(" + s0 + binary_digits_string[3] + s1 + " * 16) +" + p1;
        arithmetic_steps += p0 + "(" + s0 + binary_digits_string[4] + s1 + " * 8) +" + p1;
        arithmetic_steps += p0 + "(" + s0 + binary_digits_string[5] + s1 + " * 4) +" + p1;
        arithmetic_steps += p0 + "(" + s0 + binary_digits_string[6] + s1 + " * 2) +" + p1;
        arithmetic_steps += p0 + "(" + s0 + binary_digits_string[7] + s1 + " * 1) = " + p1;
        arithmetic_steps += p0 + parseInt(binary_digits_string[0]) * 128 + " + " + p1;
        arithmetic_steps += p0 + parseInt(binary_digits_string[1]) * 64 + " + " + p1;
        arithmetic_steps += p0 + parseInt(binary_digits_string[2]) * 32 + " + " + p1;
        arithmetic_steps += p0 + parseInt(binary_digits_string[3]) * 16 + " + " + p1;
        arithmetic_steps += p0 + parseInt(binary_digits_string[4]) * 8 + " + " + p1;
        arithmetic_steps += p0 + parseInt(binary_digits_string[5]) * 4 + " + " + p1;
        arithmetic_steps += p0 + parseInt(binary_digits_string[6]) * 2 + " + " + p1;
        arithmetic_steps += p0 + parseInt(binary_digits_string[7]) * 1 + "." + p1;
        document.getElementById("output").innerHTML += arithmetic_steps;
    }
    catch(exception) {
        console.log("An exception to expected functioning occurred during the runtime of the JavaScript function named binary_to_decimal(): " + exception);
    }
}

BINARY_TO_DECIMAL Interface (Initial)


The screenshot image below depicts what the BINARY_TO_DECIMAL web page interface is supposed to look like when the web page is initially loaded by a web browser or after the RESET button on that web page is clicked.

image_link: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/binary_to_decimal_interface_initial.png



BINARY_TO_DECIMAL Interface (Progress)


The screenshot image below depicts what the BINARY_TO_DECIMAL web page interface looks like after some of the eight binary switches are clicked.

image_link: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/binary_to_decimal_interface_progress.png



BINARY_TO_DECIMAL Interface (Final)


The screenshot image below depicts what the BINARY_TO_DECIMAL web page interface looks like after some of the eight binary switches are clicked and then the BINARY_TO_DECIMAL button is clicked.

image_link: https://raw.githubusercontent.com/karlinarayberinger/KARLINA_OBJECT_summer_2023_starter_pack/main/binary_to_decimal_interface_final.png



This web page was last updated on 10_JULY_2023. The content displayed on this web page is licensed as PUBLIC_DOMAIN intellectual property.