Introduction
A binary calculator is a program that performs arithmetic operations on binary numbers. Binary numbers are represented using only two digits, 0 and 1. In this calculator, we will implement basic binary arithmetic operations such as addition, subtraction, multiplication, and division.
Objective
The objective of this project is to build a binary calculator that takes binary input from the user and performs the following arithmetic operations:
- Binary Addition
- Binary Subtraction
- Binary Multiplication
- Binary Division
The program will output the result in binary format as well as in decimal format for easier understanding.
Go Program Code
package main
import (
"fmt"
"math"
"strconv"
)
// Function to convert binary string to decimal
func binToDec(binary string) (int, error) {
dec, err := strconv.ParseInt(binary, 2, 64)
if err != nil {
return 0, err
}
return int(dec), nil
}
// Function to convert decimal to binary string
func decToBin(decimal int) string {
return fmt.Sprintf("%b", decimal)
}
// Function for binary addition
func binaryAdd(a, b string) (string, error) {
aDec, err := binToDec(a)
if err != nil {
return "", err
}
bDec, err := binToDec(b)
if err != nil {
return "", err
}
sum := aDec + bDec
return decToBin(sum), nil
}
// Function for binary subtraction
func binarySubtract(a, b string) (string, error) {
aDec, err := binToDec(a)
if err != nil {
return "", err
}
bDec, err := binToDec(b)
if err != nil {
return "", err
}
diff := aDec - bDec
return decToBin(diff), nil
}
// Function for binary multiplication
func binaryMultiply(a, b string) (string, error) {
aDec, err := binToDec(a)
if err != nil {
return "", err
}
bDec, err := binToDec(b)
if err != nil {
return "", err
}
product := aDec * bDec
return decToBin(product), nil
}
// Function for binary division
func binaryDivide(a, b string) (string, error) {
aDec, err := binToDec(a)
if err != nil {
return "", err
}
bDec, err := binToDec(b)
if err != nil {
return "", err
}
if bDec == 0 {
return "", fmt.Errorf("division by zero")
}
quotient := aDec / bDec
return decToBin(quotient), nil
}
func main() {
var a, b string
var operation string
fmt.Println("Enter the first binary number:")
fmt.Scanln(&a)
fmt.Println("Enter the second binary number:")
fmt.Scanln(&b)
fmt.Println("Enter the operation (+, -, *, /):")
fmt.Scanln(&operation)
var result string
var err error
switch operation {
case "+":
result, err = binaryAdd(a, b)
case "-":
result, err = binarySubtract(a, b)
case "*":
result, err = binaryMultiply(a, b)
case "/":
result, err = binaryDivide(a, b)
default:
fmt.Println("Invalid operation!")
return
}
if err != nil {
fmt.Println("Error:", err)
} else {
fmt.Printf("Result in binary: %s\n", result)
resultDecimal, _ := binToDec(result)
fmt.Printf("Result in decimal: %d\n", resultDecimal)
}
}
Program Structure Explanation
The program is designed to take two binary numbers as input from the user and perform one of the four arithmetic operations: addition, subtraction, multiplication, or division. Here’s how the program works:
- The function
binToDec
is used to convert the binary string into a decimal integer. - The function
decToBin
converts the result back into binary format. - Each arithmetic operation (addition, subtraction, multiplication, division) has a corresponding function that first converts the binary inputs to decimal, performs the operation, and then converts the result back to binary.
- The
main
function handles the input and calls the appropriate operation function based on user input.
To run the program:
- Install the Go programming language if you haven’t already.
- Save the code in a file with a
.go
extension, for example,binary_calculator.go
. - Open your terminal, navigate to the directory where the file is saved, and run the command:
go run binary_calculator.go
- Enter the binary numbers and the operation when prompted, and the result will be displayed in both binary and decimal formats.