function showResults(main, secondary, label) {
var box = document.getElementById("binaryResultBox");
var mainEl = document.getElementById("mainResult");
var secEl = document.getElementById("secondaryResult");
var labelEl = document.getElementById("resultLabel");
labelEl.innerHTML = label;
mainEl.innerHTML = main;
secEl.innerHTML = secondary;
box.style.display = "block";
}
function convertBinaryToDecimal() {
var bin = document.getElementById("binaryInput").value;
if (!bin) {
alert("Please enter a binary number (0s and 1s).");
return;
}
var dec = parseInt(bin, 2);
if (isNaN(dec)) {
alert("Invalid binary input.");
return;
}
showResults(dec, "Binary: " + bin, "Decimal Equivalent");
document.getElementById("decimalInput").value = dec;
}
function convertDecimalToBinary() {
var dec = document.getElementById("decimalInput").value;
if (dec === "") {
alert("Please enter a decimal number.");
return;
}
var num = parseInt(dec);
var bin = (num >>> 0).toString(2);
showResults(bin, "Decimal: " + num, "Binary Equivalent");
document.getElementById("binaryInput").value = bin;
}
function calculateBinaryMath() {
var valA = document.getElementById("binA").value;
var valB = document.getElementById("binB").value;
var op = document.getElementById("operator").value;
if (!valA || !valB) {
alert("Please enter both binary numbers.");
return;
}
var numA = parseInt(valA, 2);
var numB = parseInt(valB, 2);
var resDec = 0;
if (op === "add") resDec = numA + numB;
else if (op === "sub") resDec = numA – numB;
else if (op === "mul") resDec = numA * numB;
else if (op === "div") {
if (numB === 0) {
alert("Cannot divide by zero.");
return;
}
resDec = Math.floor(numA / numB);
}
var resBin = (resDec >= 0) ? resDec.toString(2) : "-" + Math.abs(resDec).toString(2);
showResults(resBin, "Decimal Value: " + resDec, "Arithmetic Result");
}
Understanding the Binary System
The binary system, or base-2, is the fundamental language of modern computing. Unlike the decimal system (base-10) which uses ten digits (0-9), binary uses only two: 0 and 1. Every piece of data in a computer, from high-definition videos to simple text documents, is represented as a sequence of these two digits.
How Binary Conversion Works
To convert binary to decimal, you use powers of 2 based on the position of each digit. The rightmost digit is the 2^0 position, the next is 2^1, and so on.
Example: Convert 1011 to Decimal
- (1 × 2³) = 8
- (0 × 2²) = 0
- (1 × 2¹) = 2
- (1 × 2⁰) = 1
- Total: 8 + 0 + 2 + 1 = 11
Binary Arithmetic Rules
Performing math in binary follows similar logic to decimal math, but with much simpler "tables" for addition and multiplication:
| Operation |
Rule |
| 0 + 0 |
0 |
| 0 + 1 or 1 + 0 |
1 |
| 1 + 1 |
10 (0 carry 1) |
| 1 × 1 |
1 |
Applications of Binary
Why do we use binary? Electronic circuits are essentially composed of millions of tiny switches (transistors). A switch has two states: ON or OFF. Mapping "ON" to 1 and "OFF" to 0 provides a reliable, error-resistant way to store and process complex logic using simple physical hardware.
Our binary calculator helps students, developers, and hobbyists quickly verify their manual calculations or perform rapid conversions between binary and decimal bases without tedious manual math.