-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbinary.cpp
More file actions
65 lines (51 loc) · 1.03 KB
/
Copy pathbinary.cpp
File metadata and controls
65 lines (51 loc) · 1.03 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
/*
Nguyen Nguyen
Discrete Math Helper
October 24, 2020
*/
#include "binary.h"
Binary Binary::operator+(const Binary& binary) const
{
// I was just Too Lazy!!!
return Binary(toInteger() + binary.toInteger());
/*
Binary result;
int s = 0; //Digit sum to carry
// Traverse backward from last to first
size_t i = bin.size() - 1, j = binary.bin.size() - 1;
while (i >= 0 || j >= 0 || s == 1)
{
// sum of the last digits and carry
if (i >= 0 && bin[i] == '1')
{
++s;
}
if (j >= 0 && binary.bin[j] == '1')
{
++s;
}
result.bin.insert(0, 1, char(s % 2 + '0'));
// Update the carry
s /= 2;
i--; j--;
}
return result;
*/
}
Binary Binary::operator-(const Binary& binary) const
{
// I was just Too Lazy!!!
return Binary(toInteger() - binary.toInteger());
}
long long Binary::toInteger() const
{
return Converter::binaryStringToDec(bin);
}
std::string Binary::toString() const
{
return bin;
}
std::ostream& operator<<(std::ostream& os, const Binary& bin)
{
return os << bin.bin;
}