Results 1 to 4 of 4
  1. #1
    lobodelbosque is offline Member
    Join Date
    Nov 2009
    Posts
    12
    Rep Power
    0

    Default Sum binary numbers

    Hello:

    I wrote the following code to sum two binary numbers of the same length:

    Java Code:
    public class BinarySum {
    
    
    	public static void main(String[] args) {
    		
    		int[] a = {0,1,1,1,1,1};
    		int[] b = {0,1,1,0,1,1};
    		int[] c = new int[a.length + 1];
    		int n = a.length + 1;
    		int m = a.length;
    		int aux = 0;
    		int i;
    		
    		for(i=1 ; i <= m; i++)
    		{
    			if(a[m - i] + b[m - i] == 2)
    			{		
    				c[n-i]= 0 + aux;
    				aux = 1;
    				continue;
    				
    			}
    			
    			else
    			{
    				if(a[m - i] + b[m - i] + aux == 2)
    				{
    					c[n-i] = 0;
    					aux=1;
    					continue;
    				}						
    				
    			
    				else
    				{
    					c[n-i] = a[a.length - i] + b[b.length - i] + aux;
    					aux=0;
    				}
    				
    				
    			}
    			
    			
    		}
    		if(aux == 1)
    			c[0]=1;
    		else
    			c[0]=0;
    		for(i = 0 ; i < c.length ; i++)
    		System.out.print(c[i]);
    		
    		
    		
    		
    		
    
    	}
    
    }
    I feel it cant be shorter, i mean a way more intelligent to accomplish the task.
    Anyone have another idea?

    Thanks.
    Last edited by Fubarable; 12-13-2009 at 07:42 AM. Reason: code tags added

  2. #2
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    Java Code:
    for ndx = end downto 0
        s = a[ndx] + b[ndx] + carry
        sum[ndx + 1] = s % 2
        carry = s / 2
    sum[0] = carry

    or something...
    Last edited by pbrockway2; 12-13-2009 at 08:09 AM. Reason: code tags!

  3. #3
    lobodelbosque is offline Member
    Join Date
    Nov 2009
    Posts
    12
    Rep Power
    0

    Default

    I see thats a very shorter way.
    Thanks.

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default

    You're welcome. It's just a pseudocode description of the ordinary way we do addition (with 10 for 2): "mod" and "div" are actually old friends although they're not usually given names when teaching children to add.

Similar Threads

  1. Binary
    By THEAniKan in forum Entertainment
    Replies: 1
    Last Post: 01-06-2012, 10:14 AM
  2. Binary IO help
    By kosok in forum New To Java
    Replies: 2
    Last Post: 12-25-2008, 07:58 AM
  3. printing two smallest numbers from a series of numbers
    By trofyscarz in forum New To Java
    Replies: 2
    Last Post: 10-14-2008, 11:46 PM
  4. get binary value
    By rushenas in forum New To Java
    Replies: 2
    Last Post: 06-14-2008, 01:51 PM
  5. binary search
    By tranceluv in forum New To Java
    Replies: 10
    Last Post: 01-14-2008, 07:13 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •