Skip to content

Instantly share code, notes, and snippets.

@RahulKirtoniya
Created August 19, 2022 11:51
Show Gist options
  • Save RahulKirtoniya/88ef73fdd9a8f5bf4d3cd87237904796 to your computer and use it in GitHub Desktop.
Save RahulKirtoniya/88ef73fdd9a8f5bf4d3cd87237904796 to your computer and use it in GitHub Desktop.
When two binary strings are added, then the sum returned is also a binary string.
public class gfg {
static String add_Binary(String x, String y)
{
String res = "";
int d = 0;
int k = x.length() - 1, l = y.length() - 1;
while (k >= 0 || l >= 0 || d == 1) {
d += ((k >= 0) ? x.charAt(k) - '0' : 0);
d += ((l >= 0) ? y.charAt(l) - '0' : 0);
res = (char)(d % 2 + '0') + res;
d /= 2;
k--;
l--;
}
return res;
}
public static void main(String[] args) {
String x = "011011", y = "1010111";
System.out.println(add_Binary(x,y));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment