-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAddBinary.java
More file actions
28 lines (25 loc) · 697 Bytes
/
AddBinary.java
File metadata and controls
28 lines (25 loc) · 697 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
public class AddBinary {
public static String addBinary(String a, String b) {
StringBuilder sBuilder=new StringBuilder();
int i=a.length()-1;
int j=b.length()-1;
int carry=0;
while(i>=0||j>=0) {
int sum=carry;
if(i>=0) sum+=a.charAt(i--)-'0';
if(j>=0) sum+=b.charAt(j--)-'0';
sBuilder.append(sum%2);
carry=sum/2;
}
if(carry!=0) {
sBuilder.append(carry);
}
return sBuilder.reverse().toString();
}
public static void main(String[] args) {
// TODO Auto-generated method stub
String s1="11";
String s2="1";
System.out.println(addBinary(s1, s2));
}
}