Skip to content

Commit 76ac05c

Browse files
committed
Added Add Binary
1 parent c329092 commit 76ac05c

File tree

4 files changed

+134
-0
lines changed

4 files changed

+134
-0
lines changed
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
class BigInt {
2+
private:
3+
string binary;
4+
5+
// Helper function to ensure strings are the same length
6+
static void equalizeLength(string& a, string& b) {
7+
int lengthDifference = a.size() - b.size();
8+
if (lengthDifference > 0)
9+
b.insert(0, lengthDifference, '0');
10+
else
11+
a.insert(0, -lengthDifference, '0');
12+
}
13+
14+
// Helper function to remove leading zeros
15+
void removeLeadingZeros() {
16+
// Erase leading zeros while keeping at least one zero if the number is
17+
// zero
18+
auto firstNonZero = binary.find_first_not_of('0');
19+
if (firstNonZero != string::npos) {
20+
binary.erase(0, firstNonZero);
21+
} else {
22+
binary =
23+
"0"; // Adjust to maintain a minimum representation of zero
24+
}
25+
}
26+
27+
public:
28+
BigInt() : binary("0") {}
29+
30+
// Constructor from a binary string
31+
BigInt(const string& bin) : binary(bin) { removeLeadingZeros(); }
32+
33+
// Bitwise XOR
34+
BigInt operator^(const BigInt& other) const {
35+
string a = binary;
36+
string b = other.binary;
37+
equalizeLength(a, b);
38+
string result;
39+
for (size_t i = 0; i < a.size(); i++) {
40+
char xorChar = (a[i] == b[i] ? '0' : '1');
41+
result.push_back(xorChar);
42+
}
43+
return BigInt(result);
44+
}
45+
46+
// Bitwise AND
47+
BigInt operator&(const BigInt& other) const {
48+
string a = binary;
49+
string b = other.binary;
50+
equalizeLength(a, b);
51+
string result;
52+
for (size_t i = 0; i < a.size(); i++) {
53+
char andChar = (a[i] == '1' && b[i] == '1' ? '1' : '0');
54+
result.push_back(andChar);
55+
}
56+
return BigInt(result);
57+
}
58+
59+
// Left shift
60+
BigInt operator<<(int shift) const {
61+
string result = binary;
62+
result.append(shift, '0');
63+
return BigInt(result);
64+
}
65+
66+
// Check if BigInt is zero
67+
bool isZero() const {
68+
for (char c : binary)
69+
if (c != '0') return false;
70+
return true;
71+
}
72+
73+
// Getter for binary string
74+
string getBinary() const { return binary; }
75+
};
76+
77+
class Solution {
78+
public:
79+
string addBinary(string a, string b) {
80+
BigInt x(a);
81+
BigInt y(b);
82+
BigInt carry, answer;
83+
84+
while (!y.isZero()) {
85+
answer = x ^ y;
86+
carry = (x & y) << 1;
87+
x = answer;
88+
y = carry;
89+
}
90+
return x.getBinary();
91+
}
92+
};
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import java.math.BigInteger;
2+
3+
public class Solution {
4+
public String addBinary(String a, String b) {
5+
BigInteger x = new BigInteger(a, 2);
6+
BigInteger y = new BigInteger(b, 2);
7+
8+
while (y.compareTo(BigInteger.ZERO) != 0) {
9+
BigInteger answer = x.xor(y);
10+
BigInteger carry = x.and(y).shiftLeft(1);
11+
x = answer;
12+
y = carry;
13+
}
14+
15+
return x.toString(2);
16+
}
17+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
var addBinary = function(a, b) {
2+
let x = BigInt("0b" + a);
3+
let y = BigInt("0b" + b);
4+
5+
while (y !== 0n) {
6+
let answer = x ^ y;
7+
let carry = (x & y) << 1n;
8+
x = answer;
9+
y = carry;
10+
}
11+
12+
return x.toString(2);
13+
};
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
class Solution:
2+
def addBinary(self, a, b) -> str:
3+
x, y = int(a, 2), int(b, 2)
4+
5+
while y:
6+
answer = x ^ y
7+
carry = (x & y) << 1
8+
x, y = answer, carry
9+
10+
return bin(x)[2:]
11+
# Time: O(A + B)
12+
# Space: O(max(A, B))

0 commit comments

Comments
 (0)