This calculator helps you convert between binary and decimal numbers, and perform basic arithmetic operations on binary numbers.
Binary to Decimal Conversion
Enter a binary number (e.g., 1011) to convert it to its decimal equivalent.
Decimal Equivalent:
Decimal to Binary Conversion
Enter a decimal number (e.g., 11) to convert it to its binary equivalent.
Binary Equivalent:
Binary Arithmetic (Addition & Subtraction)
Enter two binary numbers and choose an operation to see the result in both binary and decimal.
Addition (+)
Subtraction (-)
Binary Result: Decimal Result:
.binary-numbers-calculator-container {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background-color: #f9f9f9;
padding: 25px;
border-radius: 10px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
max-width: 700px;
margin: 30px auto;
border: 1px solid #e0e0e0;
}
.binary-numbers-calculator-container h2 {
color: #2c3e50;
text-align: center;
margin-bottom: 25px;
font-size: 2em;
}
.binary-numbers-calculator-container h3 {
color: #34495e;
margin-top: 30px;
margin-bottom: 15px;
font-size: 1.5em;
border-bottom: 2px solid #3498db;
padding-bottom: 5px;
}
.calculator-section {
background-color: #ffffff;
padding: 20px;
border-radius: 8px;
margin-bottom: 20px;
border: 1px solid #dcdcdc;
}
.calculator-section p {
color: #555;
margin-bottom: 15px;
line-height: 1.6;
}
.calculator-section label {
display: block;
margin-bottom: 8px;
color: #333;
font-weight: bold;
}
.calculator-section input[type="text"],
.calculator-section input[type="number"],
.calculator-section select {
width: calc(100% – 22px);
padding: 10px;
margin-bottom: 15px;
border: 1px solid #ccc;
border-radius: 5px;
font-size: 1em;
box-sizing: border-box;
}
.calculator-section button {
background-color: #3498db;
color: white;
padding: 12px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
font-size: 1.1em;
transition: background-color 0.3s ease;
display: block;
width: 100%;
box-sizing: border-box;
margin-top: 10px;
}
.calculator-section button:hover {
background-color: #2980b9;
}
.result-display {
margin-top: 20px;
padding: 15px;
background-color: #eaf7ff;
border: 1px solid #a7d9f8;
border-radius: 8px;
color: #2c3e50;
font-size: 1.1em;
line-height: 1.8;
}
.result-display strong {
color: #2980b9;
}
.error-message {
color: #e74c3c;
font-weight: bold;
margin-top: 10px;
}
function isValidBinary(binaryString) {
return /^[01]+$/.test(binaryString);
}
function calculateBinaryToDecimal() {
var binaryInput = document.getElementById("binaryInputConvert").value.trim();
var decimalOutput = document.getElementById("decimalOutputConvert");
if (!binaryInput) {
decimalOutput.innerHTML = 'Please enter a binary number.';
return;
}
if (!isValidBinary(binaryInput)) {
decimalOutput.innerHTML = 'Invalid binary number. Only 0s and 1s are allowed.';
return;
}
var decimalValue = parseInt(binaryInput, 2);
decimalOutput.textContent = decimalValue;
}
function calculateDecimalToBinary() {
var decimalInput = document.getElementById("decimalInputConvert").value.trim();
var binaryOutput = document.getElementById("binaryOutputConvert");
if (!decimalInput) {
binaryOutput.innerHTML = 'Please enter a decimal number.';
return;
}
var decimalValue = parseInt(decimalInput, 10);
if (isNaN(decimalValue) || decimalValue < 0 || !Number.isInteger(decimalValue)) {
binaryOutput.innerHTML = 'Please enter a valid non-negative integer.';
return;
}
var binaryValue = (decimalValue >>> 0).toString(2); // Using unsigned right shift for positive integers
binaryOutput.textContent = binaryValue;
}
function performBinaryArithmetic() {
var binaryInput1 = document.getElementById("binaryInput1").value.trim();
var binaryInput2 = document.getElementById("binaryInput2").value.trim();
var operation = document.getElementById("operationSelect").value;
var arithmeticBinaryResult = document.getElementById("arithmeticBinaryResult");
var arithmeticDecimalResult = document.getElementById("arithmeticDecimalResult");
arithmeticBinaryResult.textContent = ";
arithmeticDecimalResult.textContent = ";
if (!binaryInput1 || !binaryInput2) {
arithmeticBinaryResult.innerHTML = 'Please enter both binary numbers.';
return;
}
if (!isValidBinary(binaryInput1) || !isValidBinary(binaryInput2)) {
arithmeticBinaryResult.innerHTML = 'Invalid binary number(s). Only 0s and 1s are allowed.';
return;
}
var dec1 = parseInt(binaryInput1, 2);
var dec2 = parseInt(binaryInput2, 2);
var resultDecimal;
var resultBinary;
if (operation === "add") {
resultDecimal = dec1 + dec2;
resultBinary = (resultDecimal >>> 0).toString(2);
} else if (operation === "subtract") {
resultDecimal = dec1 – dec2;
if (resultDecimal >> 0).toString(2);
}
}
arithmeticDecimalResult.textContent = resultDecimal;
arithmeticBinaryResult.textContent = resultBinary;
}
Understanding Binary Numbers
Binary numbers are the fundamental language of computers and digital electronics. Unlike the decimal system we use daily, which has ten unique digits (0-9), the binary system uses only two digits: 0 and 1. Each position in a binary number represents a power of 2, starting from 20 (which is 1) on the rightmost side.
How Binary Works
In the decimal system, each digit's position represents a power of 10 (e.g., 123 = 1*102 + 2*101 + 3*100). Similarly, in the binary system, each digit's position represents a power of 2:
… 24 (16), 23 (8), 22 (4), 21 (2), 20 (1)
For example, the binary number 1011 can be converted to decimal as follows:
1 * 23 = 1 * 8 = 8
0 * 22 = 0 * 4 = 0
1 * 21 = 1 * 2 = 2
1 * 20 = 1 * 1 = 1
Adding these values (8 + 0 + 2 + 1) gives us 11. So, binary 1011 is decimal 11.
Why Binary is Important
Computers use binary because their electronic circuits operate on two states: on (represented by 1) or off (represented by 0). This simplicity makes it incredibly efficient for processing and storing information. Every piece of data, from text and images to videos and software, is ultimately stored and manipulated as binary code.
Using the Calculator
Our Binary Numbers Calculator provides three main functionalities:
Binary to Decimal Conversion: Input any binary string (e.g., 11010) and the calculator will instantly provide its decimal equivalent.
Decimal to Binary Conversion: Enter a non-negative whole number in decimal (e.g., 26) and the calculator will convert it into its binary representation.
Binary Arithmetic: Perform basic addition or subtraction on two binary numbers. The result will be displayed in both binary and decimal formats. Note that for subtraction, if the result is negative, it cannot be represented in standard unsigned binary, and the calculator will indicate this.
This tool is perfect for students learning about computer science, electronics enthusiasts, or anyone needing quick conversions and calculations involving binary numbers.