Binary Calculator in C++
Perform binary arithmetic operations with this simple calculator
Introduction
In this program, we will create a simple binary calculator that allows users to perform arithmetic operations on binary numbers. The operations supported by this calculator will include addition, subtraction, multiplication, and division. The calculator will accept binary input, convert it to decimal, perform the required operation, and output the result in binary format.
Objective
The main objective of this program is to provide an easy-to-use tool for performing binary arithmetic and to demonstrate how binary arithmetic works under the hood. It will also highlight how binary and decimal number systems are interrelated and how binary operations are performed in C++.
Code
#include <iostream> #include <string> #include <bitset> #include <sstream> #include <cmath> using namespace std; // Function to convert binary string to decimal int binaryToDecimal(string binary) { int decimal = 0; int size = binary.size(); for (int i = 0; i < size; i++) { if (binary[i] == '1') { decimal += pow(2, size - i - 1); } } return decimal; } // Function to convert decimal to binary string string decimalToBinary(int decimal) { string binary = ""; while (decimal > 0) { binary = (decimal % 2 == 0 ? "0" : "1") + binary; decimal /= 2; } return (binary == "") ? "0" : binary; } // Main function int main() { string binary1, binary2; int choice; cout << "Enter first binary number: "; cin >> binary1; cout << "Enter second binary number: "; cin >> binary2; int num1 = binaryToDecimal(binary1); int num2 = binaryToDecimal(binary2); cout << "\nChoose operation:\n"; cout << "1. Add\n"; cout << "2. Subtract\n"; cout << "3. Multiply\n"; cout << "4. Divide\n"; cout << "Enter your choice: "; cin >> choice; int result; switch(choice) { case 1: result = num1 + num2; cout << "Result of addition: " << decimalToBinary(result) << endl; break; case 2: result = num1 - num2; cout << "Result of subtraction: " << decimalToBinary(result) << endl; break; case 3: result = num1 * num2; cout << "Result of multiplication: " << decimalToBinary(result) << endl; break; case 4: if (num2 != 0) { result = num1 / num2; cout << "Result of division: " << decimalToBinary(result) << endl; } else { cout << "Error: Division by zero is not allowed!" << endl; } break; default: cout << "Invalid choice!" << endl; break; } return 0; }
Explanation of the Program
This program implements a binary calculator that accepts two binary numbers as input and allows the user to choose an arithmetic operation to perform. The main components of the program are:
- binaryToDecimal: A function that converts a binary string (e.g., “1011”) to its decimal equivalent.
- decimalToBinary: A function that converts a decimal number back into a binary string.
- Arithmetic operations: The user can choose between addition, subtraction, multiplication, or division. The program performs the operation in decimal, then converts the result back to binary.
- Error handling: The program checks for division by zero in case the user tries to divide by zero.
How to Run the Program
To run the program:
- Write or copy the code into a C++ source file, for example,
binary_calculator.cpp
. - Compile the program using a C++ compiler (e.g., g++):
g++ binary_calculator.cpp -o binary_calculator
- Run the program by executing the compiled file:
./binary_calculator
- Enter two binary numbers and select the desired arithmetic operation to perform.