Click here to Skip to main content
15,886,422 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Java
class Node{
	
		int num;
		Node next;
	    
	    Node(int n)
	    {
	    	num=n;
	    	next = null;
	    }
	    
}   //end node

  
 class Q88
 {
 
 	
 	public static void main (String[] args)
 	{
 		
 		
 		Node head1 = toBinary(4);
		//print(head1);
		Node head2 = toBinary(2);
		//print(head2);
		
		
		double x = addBinaryNumbers(head1, head2);
		System.out.println(x);
 				
 	}//main
 	public static void print(Node top)
	    {
		while(top!=null)
		{
			System.out.printf("%d",top.num);
			top =  top.next;
		}
	    }//end print
  		public static Node toBinary(int n){
		Node np;
		Node top = null;
		Node last = null;
		
		Node xp= new Node (n);
		if (xp.num==0)return xp;
		while(n>0){
			int x = n%2;
			np = new Node(x);
			if(top==null)
				top = np;
			else 
				last.next = np;
				last = np;
				n = n/2;
		}
		return top;
		}//end function
	public static double toDecimal(Node top){
	int c = 0;
	double result = 0;
	int ans = 0;
	
	double y = Math.pow(2,2);
	
	while(top!=null)
	{
		
		result = result + (Math.pow(2,c)*top.num);	
		
		c++;
		top = top.next;
	}
	   
	return result;
}//toDecimal

	public static double addBinaryNumbers(Node top1, Node top2)
 	{
 		//declaration of variables
 		int sum = 0;
 		int carry = 0;
 		int result = 0;
 		double res = 0.0;
 		Node tail = null;
 		Node last = null;
 		Node ans  = null;
 		boolean ape = true ;
 	
 	 		while(top1!=null || top2!=null)
 		{
 			sum = top1.num + top2.num + carry;
 			System.out.println("sum" + sum);
 			carry = 0;//clear carry before next calculation
 			
 			if (sum==1 || sum == 0) 
 			{
 				
 					//System.out.println("ssssuur"+sum);
 				Node ncar = new Node (sum);
				
				if (tail==null)tail = ncar;
				else
				  last.next = ncar;
				  last = ncar;
		
				  
 			}else{
 				ans = toBinary(sum);
 				result = ans.num;
 				Node x = ans.next;
 				carry  = x.num;
 				
  				Node ncar = new Node (result);
				
				if (tail==null)tail = ncar;
				else
				  last.next = ncar;
				  last = ncar;
 				
 			}//end if;
 			
 				if ( top1.next !=null && top2.next==null) 
 			{
 				top1   = top1.next;
 				
 				System.out.println("ddddddddddddd" + top1.num);
 			}
 			
 			//get to end and both numbers are the same
 			
 			if (top1.next==null && top2.next==null && carry==1)
 			{
 				System.out.println("carrrrrrrrrrrrrr"+carry);
 				
 				Node car = new Node (carry);
 				
 				if (tail==null)tail = car;
 				else 
 					last.next = car;
 					last = car;
 				
 			}//end if
 			
 			if ( top2.next !=null && top1.next ==null) 
 		
 			{
 					top2 = top2.next;
 					System.out.println("vvvvvvvvvvvv" + top2.num);
 			}
 				
 			
 			if ( top1!=null && top2 != null)
 			{
 				System.out.println("brian");
 				top1 = top1.next;
 				top2 = top2.next;
 				
 			}
 					    
 			
 		}//while
 	/*	
 		while(tail!=null)
 		{
 			System.out.println("tail   "+tail.num);
 			tail = tail.next;
 		}
 	*/	
 		 res = toDecimal(tail); //have to send head/top
		
		return res;
 	}//addBinaryNumbers
 	
 }//class


I am converting decimal numbers to binary and adding them together. For example, I am adding 4 and 2. When the values are converted to binary, the digits are reversed, for example, 4 =001 and 2= 01. The program works if I enter two numbers the same (6, 6)it gives the correct answer, but if I enter two different numbers (4, 2) I am getting an error.



The program works up this point
top1=001
top2 =01
0+ 0 = 0 (correct)
0+ 1= 1 (correct)
Except for the last 1

Java
if ( top1.next !=null && top2.next==null) 
 	{
 		top1   = top1.next;
 	}

The structure above checks , top1 has 1 and top2 is null (no more values). I can get the number in top1.num (1) back to sum = top1.num+ top2.num+ carry, so that I cannot add the final 1 to complete the answer.

Answer returned is 2(incorrect)

Please help!
Posted
Updated 23-Aug-11 18:00pm
v4
Comments
Firo Atrum Ventus 23-Aug-11 22:30pm    
Sorry but, can you please explain more about what you are trying to achieve?
Let's say I give these values (4,2) (6,6) and (3,1), what are the expected results?
Brian Robertson 23-Aug-11 23:56pm    
1. convert numbers to binary eg
4 =001
2 =01
2. add the above together to get 011
3. then convert 011 to get 6
4. (6, 6) would give 12 (works for all numbers which are the same.
5. the program works up to a point eg: 4 = 001, 2=01, I know 01 goes correctly that's why my answer is 2. the problem is the one in 001 is not added so I can get 011.

1 solution

If you can "convert" decimal number to binary, it means they are not numbers. All numbers a binary, did you know that?
"4 = 001" means that you create a string "001" which represent a binary literal of the number on the left side of '=' (this is not assignment, just some free written form of the example). The input data can be of numeric type, but then if cannot be "decimal" or "hexadecimal" or anything related to the base, but it could be also a string representing the number; it is not very important; you first need to get a number (of numeric type, not string) and then build a string out of it.

Now, the problem is really solved in one line. Or two. What you have written has little sense. You need to use bitwise operators. See this: http://download.oracle.com/javase/tutorial/java/nutsandbolts/op3.html[^].

I will leave the coding for your homework. You are supposed to do some homework if you want to understand at least something, aren't you?

—SA
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900