public class Solution { public String addBinary(String a, String b) { int i = a.length()-1; int j = b.length()-1; int n = j>i?j:i; int sum = 0; int adv = 0; int aa; int ab; int al = i; int bl =j; StringBuffer result = new StringBuffer(); for(int ik = n ;ik>=0 ;ik--){ aa = al<0 ? 0 : a.charAt(al--)=='0'? 0:1; ab = bl<0? 0 : b.charAt(bl--)=='0'? 0:1; sum = aa + ab + adv; result.append(sum%2==1? '1':'0'); adv = sum >> 1 ; } if (adv == 1) result.append('1'); return result.reverse().toString(); } }