Adding in Java

Carrying over while adding binary numbers in Java

Page 1 of 1

1 Replies - 7372 Views - Last Post: 21 September 2007 - 10:22 AM Rate Topic: -----

#1 codur07  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 1
  • Joined: 21-September 07

Adding in Java

Post icon  Posted 21 September 2007 - 02:12 AM

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)

// 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.out.println("Enter first 3-bit sequence");
	String x =;
	System.out.println("Enter second 3-bit sequence");
	String y =;
	//By doing the following I intend to use each X digit individually with it's corresponding Y digit
	char single_character;
	//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

Is This A Good Question/Topic? 0
  • +

Replies To: Adding in Java

#2 PennyBoki  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 55
  • View blog
  • Posts: 2,345
  • Joined: 11-December 06

Re: Adding in Java

Posted 21 September 2007 - 10:22 AM

Hi I'm afraid (not really) that your program doesn't do the job you want it to.

The only adding you do is actually concatenating strings.
What is the thing you try to do with the single_character variable?

The solution is pretty easy since you have the limitation of 3bit sequence.

My suggestion is compare the last char(third) of the first string, with the last char of the second string, then do something, the same thing goes for the middle bit, but you should bear in mind if the previous comparing was comparing of two 1, the first bit comparing should go exactly as the second(middle), so the result keep it in an char array perhaps...

Now do you know how to add binaries? if no, then back to the drawing board.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1