-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbin_add.cpp
More file actions
109 lines (91 loc) · 2.94 KB
/
bin_add.cpp
File metadata and controls
109 lines (91 loc) · 2.94 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
/**
An implementation of binary addition
bin_add.cpp
@author raph-son
*/
#include <iostream>
#include <string>
using namespace std;
/*
Perform adding binary characters
@params bool - set if the current addition has remainder
char - binary characters to be added together
@return char - the result of the operation
*/
char do_add(bool& carry, char left, char right);
/*
Prefix the operand with less bit with 0's
@param string - operands
@return void
*/
void zero_prefix(string& left_operand, string& right_operand);
// Program's entry point
int main(int c, char* argv[]) {
if(c < 3) {
cout << "Usage: enter two binary to add" << endl;
return 1;
}
bool carry_switch;
string left_operand = argv[1];
string right_operand = argv[2];
// Add as much zero behind the operand with less bit where needed
zero_prefix(left_operand, right_operand);
char add_begins_left = left_operand[left_operand.length() - 1];
char add_begins_right = right_operand[right_operand.length() - 1];
string result;
// Since both side have equal length now, we can use either side
for(int i = add_begins_left; i != 0; i--) {
result.insert(0, 1, do_add(carry_switch, left_operand[i - 1], right_operand[i - 1]));
}
// If last operation has a carry, just add one the the beginning
if(carry_switch) result.insert(0, 1, '1');
cout << result << endl;
return 0;
}
// Prefix the operand with less characters with 0 to equal other length
void zero_prefix(string& left_operand, string& right_operand) {
if(left_operand.length() > right_operand.length()) {
int zero_length_to_prefix = left_operand.length() \
- right_operand.length();
for(int i = zero_length_to_prefix; i != 0; i--) {
right_operand.insert(0, 1, '0');
}
}
if(left_operand.length() < right_operand.length()) {
int zero_length_to_prefix = right_operand.length() \
- left_operand.length();
for(int i = zero_length_to_prefix; i != 0; i--) {
left_operand.insert(0, 1, '0');
}
}
}
// Determin if a binary addition involve carring or not
char do_add(bool& carry, char left, char right) {
if(carry == false) {
if(left == '0' && right == '0') {
carry = false;
return '0';
}
if( (left == '0' && right == '1') || (left == '1' && right == '0') ) {
carry = false;
return '1';
}
if(left == '1' && right == '1') {
carry = true;
return '0';
}
}
if(left == '0' && right == '0') {
carry = false;
return '1';
}
if( (left == '0' && right == '1') || (left == '1' && right == '0') ) {
carry = true;
return '0';
}
if(left == '1' && right == '1') {
carry = true;
return '1';
}
return ' ';
}