37std::string
add_strings(std::string first, std::string second) {
41 int64_t len1 = first.size();
42 int64_t len2 = second.size();
43 std::string zero =
"0";
45 for (int64_t i = 0; i < len2 - len1; i++) {
50 }
else if (len1 > len2) {
51 for (int64_t i = 0; i < len1 - len2; i++) {
58 int64_t length = std::max(len1, len2);
60 for (int64_t i = length - 1; i >= 0; i--) {
61 int64_t firstBit = first.at(i) -
'0';
62 int64_t secondBit = second.at(i) -
'0';
64 int64_t sum = (char(firstBit ^ secondBit ^ carry)) +
'0';
65 result.insert(result.begin(), sum);
67 carry = char((firstBit & secondBit) | (secondBit & carry) |
72 result.insert(result.begin(),
'1');
86std::string
safe_substr(
const std::string &str, int64_t x1, int64_t x2, int64_t n) {
87 int64_t len = str.size();
90 return str.substr(x1, x2);
93 int64_t y1 = x1 - (n - len);
94 int64_t y2 = (x1 + x2 - 1) - (n - len);
99 return str.substr(0, y2 + 1);
101 return str.substr(y1, x2);