-
Notifications
You must be signed in to change notification settings - Fork 4
/
bit_str_add.cpp
44 lines (37 loc) · 1002 Bytes
/
bit_str_add.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
/**
* Author: Skylar Payne
* Date: January 5, 2014
* Given two strings representing bits, return their sum
**/
#include <iostream>
#include <string>
//Note: This will ONLY work for properly formatted bit strings.
std::string add(std::string a, std::string b) {
int length = std::max(a.size(), b.size());
if(a.size() > b.size()) {
b = std::string(a.size() - b.size(), '0') + b;
} else if(b.size() > a.size()) {
a = std::string(b.size() - a.size(), '0') + a;
}
std::string res(length, '0');
int c = 0, i = length - 1;
do {
int bit_ai = a[i] - '0';
int bit_bi = b[i] - '0';
int bit_ci = (bit_ai ^ bit_bi ^ c) + '0';
c = ((bit_ai ^ bit_bi) & c) | (bit_ai & bit_bi);
res[i] = bit_ci;
} while(--i >= 0);
return res;
}
int main(int argc, char** argv) {
if(argc != 3) {
std::cout << "Please provide 2 bit strings to add" << std::endl;
return -1;
}
std::string a(argv[1]);
std::string b(argv[2]);
std::string c = add(a, b);
std::cout << c << std::endl;
return 0;
}