-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbinary.cpp
99 lines (87 loc) · 3.35 KB
/
binary.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include<cmath>
#include<string>
#include<sstream>
#include<bitset>
#include<stdexcept>
#define BITSET_VAL 16 // Assuming 8 bits for bitset value
using namespace std;
namespace mlb {
//function to convert decimal to binary
string decimalToBinary(int decimalNumber) {
stringstream ss;
ss << bitset<BITSET_VAL>(decimalNumber);
return ss.str();
}
// Function to convert binary to decimal
int binaryToDecimal(const string& binary) {
int decimalNumber = stoi(binary, nullptr, 2);
return decimalNumber;
}
// Function to add two binary numbers from string type input (binary1 + binary2)
string binaryAdd(const string& binary1,const string& binary2) {
bitset<BITSET_VAL> result;
bitset<BITSET_VAL>b1(binary1);
bitset<BITSET_VAL>b2(binary2);
// Perform binary multiplication
result = b1.to_ulong() + b2.to_ulong();
return result.to_string();
}
// Function to subtract two binary numbers from string type input (binary1 - binary2)
string binarySub(const string& binary1,const string& binary2) {
bitset<BITSET_VAL> result;
bitset<BITSET_VAL>b1(binary1);
bitset<BITSET_VAL>b2(binary2);
// Perform binary multiplication
result = b1.to_ulong() - b2.to_ulong();
return result.to_string();
}
// Function to multiply two binary numbers from string type input (binary1 * binary2)
string binaryMul(const string& binary1, const string& binary2) {
bitset<BITSET_VAL> result;
bitset<BITSET_VAL>b1(binary1);
bitset<BITSET_VAL>b2(binary2);
// Perform binary multiplication
result = b1.to_ulong() * b2.to_ulong();
return result.to_string();
}
// Function to divide two binary numbers from string type input (binary1 / binary2)
string binaryDiv(const string& binary1, const string& binary2) {
if (binary2 == "0") {
throw invalid_argument("Division by zero is not allowed.");
}
bitset<BITSET_VAL> result;
bitset<BITSET_VAL>b1(binary1);
bitset<BITSET_VAL>b2(binary2);
// Perform binary multiplication
result = b1.to_ulong() / b2.to_ulong();
return result.to_string();
}
// Function to add two binary numbers from int type input (binary1 + binary2)
string binaryAdd(int num1, int num2) {
string s1 = decimalToBinary(num1);
string s2 = decimalToBinary(num2);
string s3 = binaryAdd(s1, s2);
return s3;
}
// Function to subtract two binary numbers from int type input (binary1 - binary2)
string binarySub(int num1, int num2) {
string s1 = decimalToBinary(num1);
string s2 = decimalToBinary(num2);
string s3 = binarySub(s1, s2);
return s3;
}
// Function to multiply two binary numbers from int type input (binary1 * binary2)
string binaryMul(int num1, int num2) {
string s1 = decimalToBinary(num1);
string s2 = decimalToBinary(num2);
string s3 = binaryMul(s1, s2);
return s3;
}
// Function to divide two binary numbers from int type input (binary1 / binary2)
string binaryDiv(int num1, int num2) {
string s1 = decimalToBinary(num1);
string s2 = decimalToBinary(num2);
string s3 = binaryDiv(s1, s2);
return s3;
}
};