-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinary.cpp
More file actions
162 lines (139 loc) · 3.54 KB
/
Binary.cpp
File metadata and controls
162 lines (139 loc) · 3.54 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
//
// Created by Eldar on 06-Nov-18.
//
#include <string>
#include <algorithm>
#include "Binary.h"
#include <functional>
using namespace std;
/**
*
* @param num given number to constructor.
*/
Binary::Binary(const string num) : number(num) {
}
/**
*
* @return the value as string.
*/
string Binary::getValue() const {
return number;
}
/**
*
* @param string1 set the value af given string.
*/
void Binary::setValue(const string string1) {
this->number = string1;
}
/**
*
* @param number a given binary number.
* @return the logical AND.
*/
Binary Binary::operator&(const Binary &number) const {
return bitwiseOperator(number, logical_and);
}
/**
*
* @param number a given binary number.
* @return the logical AND and add it to *this.
*/
Binary &Binary::operator&=(const Binary &number) {
Binary binary = bitwiseOperator(number, logical_and);
this->number = binary.getValue();
return *this;
}
/**
*
* @param number a given binary number.
* @return the logical OR.
*/
Binary Binary::operator|(const Binary &number) const {
return bitwiseOperator(number, logical_or);
}
/**
*
* @param number a given binary number.
* @return the logical XOR.
*/
Binary Binary::operator^(const Binary &number) const {
return bitwiseOperator(number, logical_xor);
}
/**
* Creating new function of logical xor.
* @param x bool x.
* @param y bool y.
* @return xor between x and y.
*/
bool Binary::logical_xor(bool x, bool y) {
return (x || y) && !(x && y);
}
/**
* Creating new function of logical or.
* @param x bool x.
* @param y bool y.
* @return or between x and y.
*/
bool Binary::logical_or(bool x, bool y) {
return (x || y);
}
/**
* Creating new function of logical and.
* @param x bool x.
* @param y bool y.
* @return and between x and y.
*/
bool Binary::logical_and(bool x, bool y) {
return (x && y);
}
/**
* A bitwise operator for all the bitwise logic in one function.
* @param num2 a binary number.
* @param func the function we want to use (OR, AND, XOR)
* @return the relevant operator according to given function.
*/
Binary Binary::bitwiseOperator(const Binary &num2, bool(*func)(bool, bool)) const {
unsigned long int i;
string result;
// Get the strings.
string this_str = this->number;
string other_str = num2.number;
// Get the msb
string msb = "";
int offset = (int) this_str.size() - (int) num2.number.size();
// We want both of the numbers to be represented in the same bit length
if (offset < 0) {
// this_str is smaller
char m = this_str.at(0);
msb += m;
for (i = 0; i < (-1) * offset; i++) {
this_str.insert(0, msb);
}
} else if (offset > 0) {
// other_str is smaller
char m = other_str.at(0);
msb += m;
for (i = 0; i < offset; i++) {
other_str.insert(0, msb);
}
}
// this for is to add 1 or 0 to 'fill' the lower number
for (i = 0; i < this_str.size(); i++) {
if (func(this_str.at(i) == '1', other_str.at(i) == '1')) {
result += '1';
} else {
result += '0';
}
}
// Return the Binary number
return Binary(result);
}
/**
*
* @return deep clone of Binary object, means creating new string and assign the string to it.
*/
Binary Binary::deepClone() {
string temp = this->getValue();
return Binary(temp.c_str());
}