I have been trying to create a code that will add 2 strings of 3-bit binary. (e.g. 101 + 011 = 1000). I have been able to develop the majority of the code, I'm just having trouble with Carrying over when adding 1 + 1. I would really appreciate your advice on this issue.
Thank you,
Below is a copy of the code I have been working on.
(Comments are included in java code)
CODE
// This Java program should be able to add up to 3-bit binary numbers
import java.util.Scanner;
public class BinaryCalc{
public static void main(String[] args) {
Scanner keyboard = new Scanner(System.in);
System.out.println("Enter first 3-bit sequence");
String x = keyboard.next();
System.out.println("Enter second 3-bit sequence");
String y = keyboard.next();
//By doing the following I intend to use each X digit individually with it's corresponding Y digit
char single_character;
single_character=x.charAt(0);
single_character=x.charAt(1);
single_character=x.charAt(2);
single_character=y.charAt(0);
single_character=y.charAt(1);
single_character=y.charAt(2);
//The following System.out.println is where i'm having trouble, I'm not sure how to tell Java to carry over properly and tell it that 1 + 1 = 10
System.out.println(x+"+"+y+"="+(x+y));
}
}