public String addBinary(String a, String b) {
BigInteger num1 = new BigInteger(a,2);
BigInteger num2 = new BigInteger(b,2);
BigInteger zero = new BigInteger("0",2);
BigInteger carry, answer;
while(!num2.equals(zero)) {
answer = num1.xor(num2);
carry = num1.and(num2).shiftLeft(1);
num1 = answer;
num2 = carry;
}
return num1.toString(2);
}
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter