# What's wrong with my answer? It's the same as expected but judged wrong by the OJ

Input: "11", "1"
Output: "100"
Expected: "100"

``````public static String addBinary(String a, String b) {
int more;
int length = a.length();
String excess = a;
String less = a;

if(b.length() > length){
excess = b;
}
else
{
less = b;
}
Stack<Character> stack = new Stack<Character>();
int n = less.length()-1;
int m = excess.length()-1;
int sum;
more = 0;
char c;
while(n>=0){
sum = (byte) (excess.charAt(m)-'0' + less.charAt(n)-'0'+more);
if((sum&1) == 1)
{
c = '1';
}
else
{
c = '0';
}
stack.push(c);
more = (byte) (sum>>1);
n--;
m--;
}
while(m >=0){
sum = (byte)(excess.charAt(m)-'0'+more);
if((sum&1) == 1)
{
c = '1';
}
else
{
c = '0';
}
stack.push(c);
stack.push((char)(sum&1+'0'));
more = (byte) (sum>>1);
m--;
}
if(more > 0){
stack.push('1');
}
String s = new String();
while(!stack.empty()){
s += stack.pop();
}
return s;
}``````

• You are storing an unprintable character in the stack, the \0, as can be seen when you rewrite the popping section like so:

``````while(!stack.empty()){
char popped = stack.pop();
System.out.println((int)popped);
s+=popped;
}
``````

It prints out:

``````49
0
48
48
``````

or 1 nil 0 0.

While the nil is not printable, it makes .equals() return false.

• Thank you. I've checked my answer and found that I've made a mistake here:
if((sum&1) == 1)
{
c = '1';
}
else
{
c = '0';
}
stack.push(c);
stack.push((char)(sum&1+'0'));