import java.util.Scanner;
/*AniBlogger12
* Binary Addition Program
*/
public class binaryAddition {
public static void main(String[] args) {
// get keyboard
Scanner keyboard = new Scanner(System.in);
// prompt user
System.out.println("Enter a binary number.");
// user integer number input
int num1 = keyboard.nextInt();
// convert the int number to a string
String number1 = Integer.toString(num1);
// make length variable and set it equal to the length of the number entered
int length1 = number1.length();
// prompt user asking for another binary number
System.out.println("Enter another binary number.");
// user input for another binary number
int num2 = keyboard.nextInt();
// convert the int number to a string
String number2 = Integer.toString(num2);
// make length variable and set it equal to the length of the number entered
int length2 = number2.length();
// add the two binary numbers like adding normal numbers
int add = num1 + num2;
// convert sum to a string (this is not the answer)
String notanswer = Integer.toString(add);
// add a zero to the string variable
notanswer = "0" + notanswer;
// find length of this
int length = notanswer.length();
// make a string digit variable
String digit = "";
// make an answer variable
String answer = "";
// make and initialize a carry variable
int carry = 0;
// if the length of the first num is greater than the length of the second num
if (length1 > length2) {
// for loop
// iterate forward
for (int i = 0; i <= length1; i++) {
// take part of the digit from length - i - 1th position to length -ith position
digit = notanswer.substring(length - i - 1, length - i);
// if first digit is 0 and there is nothing to carry over
if (digit.equals("0") && carry == 0) {
// add a 0 to the answer
answer = "0" + answer;
// carry stays 0
carry = 0;
}
// or else if the first digit is 0 and carry is 1
else if (digit.equals("0") && carry == 1) {
// add 1 to the answer
answer = "1" + answer;
// carry becomes 0
carry = 0;
}
// or else if the first digit is 1 and carry is 0
else if (digit.equals("1") && carry == 0) {
// add 1 to the answer
answer = "1" + answer;
// carry over 0
carry = 0;
}
// else if the first digit is 1 and carry is 1
else if (digit.equals("1") && carry == 1) {
// add 0 to answer
answer = "0" + answer;
// carry over 1
carry = 1;
}
// else if the first digit is 2 and carry is 0
else if (digit.equals("2") && carry == 0) {
// add 0
answer = "0" + answer;
// carry over 1
carry = 1;
}
// else if the first digit is 2 and carry is 1
else if (digit.equals("2") && carry == 1) {
// add 1 to answer
answer = "1" + answer;
// carry over 1
carry = 1;
}
}
}
// or else
// if first number is smaller than second number
else if (length1 < length2) {
// for-loop
for (int i = 0; i <= length2; i++) {
// same as last time
digit = notanswer.substring(length - i - 1, length - i);
// same as before
if (digit.equals("0") && carry == 0) {
answer = "0" + answer;
carry = 0;
}
else if (digit.equals("0") && carry == 1) {
answer = "1" + answer;
carry = 0;
}
else if (digit.equals("1") && carry == 0) {
answer = "1" + answer;
carry = 0;
}
else if (digit.equals("1") && carry == 1) {
answer = "0" + answer;
carry = 1;
}
else if (digit.equals("2") && carry == 0) {
answer = "0" + answer;
carry = 1;
}
else if (digit.equals("2") && carry == 1) {
answer = "1" + answer;
carry = 1;
}
}
}
// or else if both are equal
else if (length1 == length2) {
// for-loop
for (int i = 0; i <= length2; i++) {
// same as before
digit = notanswer.substring(length - i - 1, length - i);
if (digit.equals("0") && carry == 0) {
answer = "0" + answer;
carry = 0;
}
else if (digit.equals("0") && carry == 1) {
answer = "1" + answer;
carry = 0;
}
else if (digit.equals("1") && carry == 0) {
answer = "1" + answer;
carry = 0;
}
else if (digit.equals("1") && carry == 1) {
answer = "0" + answer;
carry = 1;
}
else if (digit.equals("2") && carry == 0) {
answer = "0" + answer;
carry = 1;
}
else if (digit.equals("2") && carry == 1) {
answer = "1" + answer;
carry = 1;
}
}
}
// print out the final sum
System.out.println(answer);
}
}